Data Structures & AlgorithmsEasyTCS NQT Digital · Product Company SDE

Problem: String t is string s shuffled with one extra letter added. Find the extra letter by XOR-ing all character codes.

Problem: String t is string s shuffled with one extra letter added. Find the extra letter by XOR-ing all character codes.
from functools import reduce
def extra(s, t):
    return chr(reduce(lambda x, c: x ^ ord(c), s + t, 0))
print(extra("plan", "nalpe"), extra("zz", "zzy"))
What is printed?
  1. A. p y
  2. B. n y
  3. C. e y
  4. D. e z

Correct answer: C. e y

x ^ x = 0, so every letter appearing in both strings cancels and only the extra code survives – even with duplicates such as "zz". O(n) time, O(1) space; sorting both strings would be O(n log n).

More Coding Interview Problems questions

Exams that ask this topic