GATE / CS ARCHIVE

GATE CSE 2002 / QUESTION 2.10

Random sampling search with replacement

Algorithms · Searching, Sorting & Selection · Full GATE CSE 2002 paper

GATE 2002, Q2.10 2 marks · MCQ
Report issue

Searching, Sorting & Selection

Question 2.10, GATE 2002: An unsorted array A[1..n] contains n distinct values and contains x. A search repeatedly chooses i uniformly from 1..n, compares A[i] with x, stops on equality, and otherwise repeats the random choice. What is its expected comparison count before stopping?
(A) n; (B) n−1; (C) 2n; (D) n/2.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

Step 1: Recognize independent repeated trials.

Step 2: After any failed draw, the algorithm restarts with the same success probability 1/n; it has not removed that position from future draws. Thus the comparison count is geometric, P(N=k)=(1−1/n)k−1/n. Its mean is 1/(1/n)=n, agreeing with the recurrence E=1+(1−1/n)E.

Conclusion: Each independently sampled index hits the unique target with probability 1/n. If E is the expected number of comparisons including the successful comparison, E=1+(1−1/n)E, giving E=n, A. Resampling already examined positions is allowed; this is not a random permutation scan.

Original paper, page 4