GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer about Algorithms, Bit Magic

(16 posts)
  • Started 10 months ago by lucky
  • Latest reply from Aashish Barnwal
  1. lucky
    guest
    Posted 10 months ago #

    An array A[1...n] contains all the integers from 0 to n except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is “fetch the jth bit of A[i]”, which takes constant time. Write code to find the missing integer. Can you do it in O(n) time?

  2. rajcools
    Member
    Posted 10 months ago #

    divide the numbers based on bits
    first 0th bit -- all odd numbers (having 0th bit 1) and even numbers(having 0th bit 0)
    we are left with 2 parts
    find umber from which part is missing
    then in that subpart check for 1st bit again by dividing number into two parts

    complexity - n+n/2+n/4+n/8 so on approx equal to 2n = O(n)

    with an example

    lets take number upto
    5 with 3 missing

    1 - 001
    2- 010
    // 3-011(missing)
    4-100
    5-101
    6-110
    divide into two parts

    1 -001 2 -010
    5 -101 4 -100
    6-110
    one from left side is missing
    now apply algo using 1st bit
    001
    101
    as both bits are zero 1st bit of missing number should be 1
    so we have 11 known now for 2nd bit again apply algo
    as there is no number with 1st bit 1 other than this number in this series so 2nd bit shall be zero as number r starting frm 1 and number is 011 i.e 3

  3. Shandon
    guest
    Posted 10 months ago #

    XOR all j bits for A[1..n] together.
    The result is the missing number.

  4. rajcools
    Member
    Posted 10 months ago #

    @shandon your complexity will be O(jn) not O(n) and j has to be the bits in highest value of a[i] here

  5. Shandon
    guest
    Posted 10 months ago #

    @raj..This is true. Could you explain code-wise what you mean by dividing into two parts broseph?

  6. Shandon
    guest
    Posted 10 months ago #

    Nevermind I get it.

  7. raja
    guest
    Posted 10 months ago #

    @rajcool explin
    what it is
    "find umber from which part is missing
    then in that subpart check for 1st bit again by dividing number into two parts"

    what u r doing here find umber from which part is missing
    then in that subpart check for 1st bit again by dividing number into two parts

  8. rajcools
    Member
    Posted 10 months ago #

    @raja -- i am dividing the number into two parts one part in which particular bit is 1 and other in which that bit is 0
    the part that contain fewer elements contains missing element[you ofcourse have to take into consideration whether number of elements are odd or even at start]
    we first check for 0th bit divide given array into two parts
    then on part in which missing element is we apply same algo for 1st bit again divide into parts and then repeat process for 2nd bit and so on

  9. aks
    guest
    Posted 10 months ago #

    what if the nos are odd?

  10. aks
    guest
    Posted 10 months ago #

    i mean if the number of elements is odd ?

  11. hatella
    guest
    Posted 3 months ago #

    An array A[1 n] contains all the integers from 0 to n except one. It would be easy
    to determine the missing integer in O(n) time by using an auxiliary array B[0 n]
    to record which numbers appear in A. In this problem, however, we cannot
    access an entire integer in A with a single operation. The elements of A are
    represented in binary, and the only operation we can use to access them is
    "fetch the jth bit of A[i]," which takes constant time. Show that if we use only
    this operation, we can still determine the missing integer in O(n) time.
    1.Parse all the element of the array looking for it zeroth bit.
    2.Calculate number of zeros and ones encountered and its index in the array.
    3.if number of zeros are greater than ones
    4.Parse all the element whose ending bit is 1. and look for it's second last bit.
    5.Repeat the above steps for number of bits.

  12. Raju
    guest
    Posted 3 months ago #

    @hatella:
    According your algo i hope time complexity is not O(n) it is O(jn) isn't it ?

  13. atul007
    Member
    Posted 3 months ago #

    didnt get it :( @rajacool ...btw will your algo work if arr[] has element in unsorted order???

  14. PsychoCoder
    Member
    Posted 3 months ago #

    Here j is constant for 32 bit numbers!!

    So basically it still O(n) !!

  15. Dedicated Programmer
    Member
    Posted 1 month ago #

    @rajcools , i am a little bit confused.
    What to do if the number of elements is odd, i mean it has not been mentioned in the question that n will be even.
    So,in this case,dividing the array into two halves will always result into either
    (number of 0's > number of 1's) or
    (number of 1's > number of 0's).
    So, what to do in this case.

  16. Dedicated Programmer
    Member
    Posted 1 month ago #

    @Shandon , here elements can be accessed bit by bit,
    So,before Xoring all the elements,first we need to access them one by one and store them somewhere.
    Don't you think that it will result into extra space complexity.


Reply

You must log in to post.

RSS feed for this topic