Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: Merge sort's merge step in Java uses `if (a[i] < b[j]) take a[i]; else take b[j];`. What property is lost compared with `<=`?
- A. Correct sorting – the output may be unsorted
- B. Stability – equal keys from the right half can jump ahead of equal keys from the left half
- C. O(n log n) time – it becomes O(n²)
- D. Nothing – both are identical for every input
Correct answer: B. Stability – equal keys from the right half can jump ahead of equal keys from the left half
On ties, < takes from the right half first, so equal elements change relative order. The result is still sorted and still O(n log n), but stability matters when sorting records by multiple keys.
More Coding Interview Problems questions
- Problem: The 'two-sum on a sorted array' code below has a subtle edge-case behaviour. What does it print? python def twosumsorted(a, t)…
- Problem: A ride app must repeatedly add drivers with ratings and remove the highest-rated driver, n = 10⁵ operations. Which data structure…
- Problem: A leaderboard needs: insert a score, delete a score, and 'how many scores are below x?' – all online, 10⁵ operations, scores up…
- Problem: A browser keeps history so that 'back' and 'forward' work like the real buttons, and visiting a new page clears the forward…
- Problem: For a stream of n = 10⁶ (user, timestamp) hits, answer 'how many hits in the last 300 seconds?' after each new hit (timestamps…
- Problem: Implement a set that supports insert, delete and getRandom (uniformly random element) all in average O(1). Which combination works?