GATE / CS ARCHIVE

GATE CSE 2000 / QUESTION 15(c)

Prove sparse-set rejection

Data Structures · Arrays & Linked Lists · Full GATE CSE 2000 paper

GATE 2000, Q15(c) Descriptive
Report issue

Arrays & Linked Lists

Question 15(c), GATE 2000: Arrays p[1..N] and q[1..N] initially contain arbitrary values; count=0. Pseudocode:
set(i) { count=count+1; q[count]=i; p[i]=count; }
is_set(i) { if (p[i]<=0 or p[i]>count) return false; if (q[p[i]]!=i) return false; return true; }
Assume valid indices and no capacity overflow. Prove is_set(i) returns false if set(i) has never been called, regardless of the initial p[i].
Question 15(c), GATE 2000: Arrays p[1..N] and q[1..N] initially contain arbitrary values; count=0. Pseudocode:
set(i) { count=count+1; q[count]=i; p[i]=count; }
is_set(i) { if (p[i]<=0 or p[i]>count) return false; if (q[p[i]]!=i) return false; return true; }
Assume valid indices and no capacity overflow. Prove is_set(i) returns false if set(i) has never been called, regardless of the initial p[i].
Question 15(c), GATE 2000: Arrays p[1..N] and q[1..N] initially contain arbitrary values; count=0. Pseudocode:
set(i) { count=count+1; q[count]=i; p[i]=count; }
is_set(i) { if (p[i]<=0 or p[i]>count) return false; if (q[p[i]]!=i) return false; return true; }
Assume valid indices and no capacity overflow. Prove is_set(i) returns false if set(i) has never been called, regardless of the initial p[i].
Tap to enlarge
Answer & solution

Answer: Either p[i] is out of the initialized range, or q[p[i]] differs from i. (no official key was published)

There are two exhaustive cases. First, p[i]<=0 or p[i]>count: the first guard returns false before q is indexed, so an arbitrary invalid p[i] cannot cause an out-of-range read. Second, 1<=p[i]<=count: every cell in q[1..count] was explicitly written by some set(j) call and stores that call’s j. Since set(i) never occurred, none of these cells equals i. In particular q[p[i]]!=i, so the second guard returns false. This argument also covers count=0, when every possible integer p[i] fails the range guard. The reverse-pointer check is what prevents a coincidentally plausible initial p[i] from producing a false positive. This is an abstract-array algorithm, not permission to read indeterminate C objects: a literal C implementation must use a representation for which such reads are defined. Calls must stay within the arrays’ capacity; otherwise the premise that q[1..count] is valid fails.

Original paper, page 8