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")
  1. A. true false true
  2. B. false false false
  3. C. true false false
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic