none
Compare two int Array with location of number RRS feed

  • Question

  • Compare two arrays A and B one number at a time. Array A has numbers from 1 to 16 and Array B has number 4,8,12 and 15. one number should be compared at a time. In case of no match show 0 if matches show number. Next number in B must be compared from next number to the previous match. Ex- If previous match was at N+3 next comparison must start from N+4 and result should show 0,0,0,4,0,0,0,8,0,0,0,12,0,0,15,0. Looking to be pointed to right logic
    Tuesday, April 16, 2019 8:13 PM

All replies

  • Compare two arrays A and B one number at a time. Array A has numbers from 1 to 16 and Array B has number 4,8,12 and 15. one number should be compared at a time. In case of no match show 0 if matches show number. Next number in B must be compared from next number to the previous match. Ex- If previous match was at N+3 next comparison must start from N+4 and result should show 0,0,0,4,0,0,0,8,0,0,0,12,0,0,15,0. Looking to be pointed to right logic

    The "right logic" should be found within your own capabilities and creativity.
    Programming is about applied logic and creative solutions. Not about
    plagiarizing the ideas of others. Academic exercises and tests such as this
    one are intended to exercise and evaluate *your* mastery of the material
    covered to date.

    We don't do students' assignments here, as I trust you can appreciate.
    But if you make am attempt at programming the assignment we'll be quite
    willing to help steer you toward a solution.

    For your own benefit, avoid using any complete solutions that overzealous
    posters may offer. You'll learn more by working through the details on your own.

    - Wayne

    Tuesday, April 16, 2019 9:51 PM
  • One way to do so is to "merge" two arrays into one by comparing and insert, then walk the new array by comparing each element with next element.

    When the numbers are the same, then delete the next element. If the numbers are different, set the current one to be "0".

    Of course this is flawed way to do so, because if the numbers in arrays can duplicate, you can't tell if the "same number" case comes from the same array or the other. So this method have 2 assumptions:

    1) Both arrays are ordered

    2) The numbers in the same array must be unique.

    Also, the performance is not the best because you need to allocate a new array with size equal to the sum of length in both array.

    However, it serves as nice start point for you to optimize it and remove the listed limitations in the method.


    P.S.: Providing intentionally written-to-be-problematic code and let candidate decide how to improve it is also one of my favorite interview question type. 
    Wednesday, April 17, 2019 1:44 AM
    Answerer