GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Check binary-search endpoint progress and equality handling

4 questions solved the same way, newest first. Back to Programming in C

How these are solved

All require reading an actual binary-search implementation, tracing midpoint comparisons and checking whether endpoints strictly shrink while a hit remains detectable.

2 marks · MCQ
Report issue

Control Flow & Loop Tracing

Question 42, GATE 2014: A sorted ascending array listA has n>0 entries. ProcessArray sets i=0,j=n−1, then repeatedly sets k=(i+j)/2 using integer division; if x≤listA[k], sets j=k−1; independently, if listA[k]≤x, sets i=k+1; repeats while i≤j. Afterward returns k if listA[k]==x, else −1. Which is correct? A absent x causes an infinite loop; B this is binary search; C it always finds the maximum; D it returns −1 even when x is present.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Step 1: If x is smaller than the midpoint, only the first comparison succeeds, eliminating the midpoint and upper half. If x is larger, only the second succeeds, eliminating the lower half. Each unsuccessful iteration strictly shrinks the candidate interval. Step 2: If x equals the midpoint, both independent tests succeed: j=k−1 and i=k+1. The loop terminates immediately, and the final equality test returns the matching k. Step 3: Therefore it implements binary search, B, including for duplicates where any matching index suffices. Assume a valid nonempty sorted array and arithmetic without overflow in i+j.

Original paper, page 63 · Answer key

2 marks · MCQ
Report issue

Nontermination in a binary-search implementation

Control Flow & Loop Tracing

Question 84, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2.   int i,j,k;
3.   i=0; j=9;
4.   do {
5.     k=(i+j)/2;
6.     if(Y[k]<x) i=k; else j=k;
7.   } while((Y[k]!=x) && (i<j));
8.   if(Y[k]==x) printf("x is in the array");
9.   else printf("x is not in the array");
10. }
For which input does it fail?

(A) Y=[1,2,3,4,5,6,7,8,9,10], x<10
(B) Y=[1,3,5,7,9,11,13,15,17,19], x<1
(C) Y=[2,2,2,2,2,2,2,2,2,2], x>2
(D) Y=[2,4,6,8,10,12,14,16,18,20], 2<x<20 and x even
Question 84, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2.   int i,j,k;
3.   i=0; j=9;
4.   do {
5.     k=(i+j)/2;
6.     if(Y[k]<x) i=k; else j=k;
7.   } while((Y[k]!=x) && (i<j));
8.   if(Y[k]==x) printf("x is in the array");
9.   else printf("x is not in the array");
10. }
For which input does it fail?

(A) Y=[1,2,3,4,5,6,7,8,9,10], x<10
(B) Y=[1,3,5,7,9,11,13,15,17,19], x<1
(C) Y=[2,2,2,2,2,2,2,2,2,2], x>2
(D) Y=[2,4,6,8,10,12,14,16,18,20], 2<x<20 and x even
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. With all entries equal to 2 and x>2, every comparison takes i=k. The interval evolves (i,j)=(0,9)→(4,9)→(6,9)→(7,9)→(8,9), then k remains 8 forever.
2. Y[k]≠x and i<j both stay true, so the program fails to terminate, option C. For A, all integer keys below 10 that are present (1 through 9) can be found and keys below 1 terminate as absent; B also shrinks j to zero and terminates absent; D contains only present even values strictly between its endpoints.

Original paper, page 18

2 marks · MCQ
Report issue

Repairing the erroneous binary search

Control Flow & Loop Tracing

Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2.   int i,j,k;
3.   i=0; j=9;
4.   do {
5.     k=(i+j)/2;
6.     if(Y[k]<x) i=k; else j=k;
7.   } while((Y[k]!=x) && (i<j));
8.   if(Y[k]==x) printf("x is in the array");
9.   else printf("x is not in the array");
10. }
Which correction makes the program work properly?

(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));
Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2.   int i,j,k;
3.   i=0; j=9;
4.   do {
5.     k=(i+j)/2;
6.     if(Y[k]<x) i=k; else j=k;
7.   } while((Y[k]!=x) && (i<j));
8.   if(Y[k]==x) printf("x is in the array");
9.   else printf("x is not in the array");
10. }
Which correction makes the program work properly?

(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));
Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2.   int i,j,k;
3.   i=0; j=9;
4.   do {
5.     k=(i+j)/2;
6.     if(Y[k]<x) i=k; else j=k;
7.   } while((Y[k]!=x) && (i<j));
8.   if(Y[k]==x) printf("x is in the array");
9.   else printf("x is not in the array");
10. }
Which correction makes the program work properly?

(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));
Tap to enlarge
Your answer
Answer & solution

Answer: No fully correct option; A repairs the update but is incomplete (no official key was published)

1. Option A changes the bounds to i=k+1 or j=k−1, removing the nonprogress bug in Q84. But line 7 still tests i<j, so it can stop before examining a remaining singleton. Counterexample: Y=[1,2,3,4,5,6,7,8,9,10], x=4.
2. With A, k=4 gives j=3; k=1 gives i=2; k=2 gives i=3. Now i=j=3, so the loop exits and wrongly reports absent because its last examined value was Y[2]=3. A complete repair also changes the continuation bound to i<=j (while retaining the found test), or rewrites the usual while(i<=j) search.
3. B expands bounds, C still stalls on many absent keys, and D continues on equality instead of inequality. Thus A is the likely intended correction, but none alone makes the displayed program generally correct.

Original paper, page 18

5 marks · Descriptive
Report issue

Failure conditions of a binary search

Control Flow & Loop Tracing

Question 18, GATE 1996: For N>1, the following Pascal-like program searches an array a[1..N]: initialize i=1,j=N; repeatedly set k=floor((i+j)/2), then if a[k]<x set i=k, otherwise set j=k; stop when a[k]=x or i>=j. Finally print success if a[k]=x and failure otherwise. Determine when it fails. For the intended binary-search interpretation assume a is sorted in nondecreasing order. GFG prints the success message correctly; GateForum instead prints failure in both final branches.
Question 18, GATE 1996: For N>1, the following Pascal-like program searches an array a[1..N]: initialize i=1,j=N; repeatedly set k=floor((i+j)/2), then if a[k]<x set i=k, otherwise set j=k; stop when a[k]=x or i>=j. Finally print success if a[k]=x and failure otherwise. Determine when it fails. For the intended binary-search interpretation assume a is sorted in nondecreasing order. GFG prints the success message correctly; GateForum instead prints failure in both final branches.
Tap to enlarge
Answer & solution

Answer: It loops when x>a[1] and x is not in positions 1..N−1; in particular a unique match at the last element is missed (no official key was published)

The defect is that the lower update keeps the already examined element: i=k instead of i=k+1. When j=i+1, integer division gives k=i. If a[i]<x, setting i=k changes nothing; j also stays unchanged, a[k] is not x, and i<j remains true. The loop repeats forever.

In fact the final array position N is never examined: while i<j, floor((i+j)/2)<j<=N, so k<=N−1. On a nondecreasing array, the exact classification is: every x occurring among a[1],...,a[N−1] is eventually found; every x<a[1] terminates with failure; every x>a[1] absent from a[1..N−1] loops forever. The latter includes absent values between array entries, values above the maximum, and x=a[N] when that value occurs only at the last position. Duplicates matter: x=a[N] is found if the same value occurs earlier.

For example a=[1,3], x=3 gives (i,j,k)=(1,2,1) forever. With x=0, the first iteration instead sets j=1 and terminates correctly. For a=[1,3,5], x=3 is examined immediately and succeeds. These examples distinguish the actual bug from the inaccurate claim that all unsuccessful searches fail.

A robust repair uses while i<=j, tests equality first, then sets i=k+1 when a[k]<x and j=k−1 otherwise, reporting failure after the interval becomes empty. This also permits examining N.

Original paper, page 9