Data Structures & AlgorithmsVery hardProduct company OA
A monotonic stack removes duplicate letters so that each letter appears exactly once and the result is the lexicographically SMALLEST possible subsequence. What is the result for 'dbcabdc'?
- A. abdc
- B. abcd
- C. bcad
- D. dbca
Correct answer: A. abdc
Pop a larger letter on top only if it appears again later. d is pushed; b pops d (d reappears at 5); c is pushed → [b, c]; a pops c and b (both reappear) → [a]; then b and d are pushed → [a, b, d]; the final c cannot pop d because no d comes later → 'abdc'. 'abcd' is impossible: after the last d (index 5) only c follows, so d must come before the final c.
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
- 20 keys are hashed independently and uniformly into 100 slots. What is the EXPECTED number of colliding pairs (unordered pairs of keys…
- How many contiguous subarrays of [1, 3, 2, 4, 6, 5] satisfy max − min ≤ 2? (Two monotonic deques shrink the window from the left.)
- What is the smallest positive integer missing from [3, 4, −1, 1, 7, 2, 2, 9]? (In-place cyclic placement: put each value v with 1 ≤ v ≤ n…
- Which problem CANNOT be solved by a plain variable-size sliding window that shrinks from the left?
- The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 5, 5, 7, 7, 7, 7, 7, 3]. What candidate and final…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…