Which replacement maps expose cascading substitutions
Arrays & Strings
![Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.](../../../assets/questions/CS-2013-Q60-1.webp)
![Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.](../../../assets/questions/CS-2013-Q60-2.webp)
Answer & solution
Official answer: B
The defect is that the inner loop continues after a match, so a replacement character can match a later oldc entry and be replaced again. In case 1 (abc→dab), a becomes d, b becomes a after the a-entry has already been passed, and c becomes b after the b-entry has been passed. No input character cascades. In case 2 (cde→bcd), each replacement is absent from the remaining suffix of oldc, so again no character cascades. In case 3 (bca→cda), an original b first becomes c and then becomes d; the intended one-step result is c. In case 4 (abc→bac), an original a first becomes b and then becomes a; the intended result is b. Exhaustive length-five input testing includes strings containing b and a, respectively, so exactly cases 3 and 4 can reveal the flaw. That is two cases, option B. The five-character array need not be NUL-terminated because the code accesses exactly its five given elements.
The intended specification performs one replacement based on the original character, whereas the code repeatedly tests the updated character. For each map, inspect whether a replacement equals an oldc entry at a later j. Only later entries matter: a match with an earlier entry is never revisited. Map3 has b→c with c later, and map 4 has a→b with b later. Maps 1and2 only map backward or outside the remaining entries, so no repeated substitution can occur. Exhaustive input testing guarantees witnesses containing the affected characters.

