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?- A. p y
- B. n y
- C. e y
- 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
- Problem: Look-and-say: read the previous term aloud in runs ("1" → "11" → "21" …). python def say(s): out, i = [], 0 while i < len(s): j =…
- Problem: Write a string in a zigzag over r rows (down, then diagonally up) and read it row by row. python def zigzag(s, rows): if rows ==…
- Problem: Compare dotted version strings numerically part by part (missing parts count as 0); return 1, 0 or −1. python def cmpver(a, b): x…
- Problem: Multiply two non-negative integers given as decimal strings without converting them to int; digit i·j lands in position i + j + 1…
- Problem: Reverse the bit order of an 8-bit value (bit 0 ↔ bit 7, …). python def rev8(x): r = 0 for in range(8): r = (r << 1) | (x & 1); x…
- Problem: Java int shifts: arithmetic >> keeps the sign, logical >>> fills with zeros, and shifting into the sign bit overflows. java int a…