Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version B checks only s → t. What is printed?
Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version B checks only s → t. What is printed?
def iso(s, t, both=True):
m1, m2 = {}, {}
for a, b in zip(s, t):
if m1.setdefault(a, b) != b: return False
if both and m2.setdefault(b, a) != a: return False
return True
ps = [('egg', 'add'), ('badc', 'baba'), ('paper', 'title'), ('aab', 'xyy'), ('foo', 'bar')]
print(sum(iso(s, t) for s, t in ps), sum(iso(s, t, False) for s, t in ps))- A. 2 4
- B. 3 2
- C. 1 3
- D. 2 3
Correct answer: D. 2 3
A one-way map lets two different letters map to the same letter (e.g. 'ab' → 'aa'); a bijection needs the reverse map too. Output: 2 3.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A monotonic deque reports the maximum of every window of size 3 in [0, 13, 9, 1, 2, 18, −1, −3, 17]. What is the SUM of all reported…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0] together into…
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " new the is oil " w = s.split() print("|" + "…
- You must answer many queries 'sum of A[l..r]' while also updating single elements, on an array of n elements. Which choice gives O(log n)…
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 7, 4, 3, 2, 1, 1]. Treating i → a[i] as a linked…