ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function iso(String a, String b)
Map f = empty, g = empty
for (each i from 0 to length(a) - 1)
if (f contains a[i] AND f[a[i]] != b[i]) return false
if (g contains b[i] AND g[b[i]] != a[i]) return false
f[a[i]] = b[i]
g[b[i]] = a[i]
end for
return true
End Function
Print iso("egg", "add"), iso("foo", "bar"), iso("ab", "aa")- A. true false true
- B. false false false
- C. true false false
- D. true true false
Correct answer: C. true false false
The two maps make the letter correspondence one-to-one in both directions. egg ↔ add works (e↔a, g↔d). foo → bar fails (o would map to both a and r). ab → aa fails the reverse map (a would come from both a and b). Output true false false.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "AEBCBDA" String t = reverse of s Integer n =…
- What will be the output of the following pseudocode? Integer p[6] = {7, 1, 5, 3, 6, 4} Integer lo = p[0], one = 0, many = 0 for (each i…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {2, 3, 1, 1, 4} Integer jumps = 0, curEnd…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer h[9] = {1, 8, 6, 2, 5, 4, 8, 3, 7} Integer i = 0…
- What will be the output of the following pseudocode? (>> is right shift, & is AND) Integer a[3] = {4, 14, 2}, total = 0 for (each b from 0…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) Integer x = 1, r = 0 for (each i from 1 to…