Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: A stream of integers arrives: 4, 9, 1, 7, 8, 3, 6. After each arrival from the 2nd onwards, we report the 2nd largest value seen so far (it is undefined after the first arrival). What is the sum of the reported values?
- A. 54
- B. 43
- C. 39
- D. 27
Correct answer: C. 39
Tracking the top two values, the reports are 4, 4, 7, 8, 8, 8, summing to 39. Reporting the maximum instead gives 6 × 9 = 54, also counting the first arrival 4 gives 43, and tracking the 2nd smallest gives 27.
More Coding Interview Problems questions
- Problem: How many binary search trees can be formed with exactly 5 distinct keys?
- Problem: The quadratic residues modulo 8: how many integers x in 1 ≤ x ≤ 100 satisfy x² ≡ 1 (mod 8)?
- Problem: Dijkstra's shortest distances from source 0 in the directed graph 0→1 (3), 0→2 (1), 2→1 (1), 1→3 (2), 2→3 (6), 3→4 (1), 1→4 (9)…
- Problem: The number of paths from the bottom-left to the top-right corner of a 4×4 grid of points, moving only right, up, or diagonally…
- Problem: Find the number of inversions of the sequence 2, 4, 1, 3, 5, 0 processed with merge sort (count each cross-pair during merging).
- Problem: A linked list is written as values 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8. The list is reversed in groups of 3 (the last group of fewer…