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].](../../assets/questions/CS-2000-Q79-1.webp)
![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].](../../assets/questions/CS-2000-Q79-2.webp)
![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].](../../assets/questions/CS-2000-Q79-3.webp)
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.