Data Structures & AlgorithmsEasyAccenture · Cognizant coding round pattern
Problem: A ride app must repeatedly add drivers with ratings and remove the highest-rated driver, n = 10⁵ operations. Which data structure is best and why?
- A. A sorted array – O(1) insert
- B. A queue – FIFO gives the best driver
- C. A hash map – O(1) maximum lookup
- D. A max-heap (priority queue) – O(log n) insert and extract-max
Correct answer: D. A max-heap (priority queue) – O(log n) insert and extract-max
A heap keeps the maximum at the root with logarithmic updates. A sorted array needs O(n) shifting per insert, a queue ignores ratings, and a hash map has no ordering, so finding the maximum is O(n).
More Coding Interview Problems questions
- 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?
- Problem: Support 'add number' and 'find a pair summing to t' where adds are 10⁵ and finds are rare, versus the opposite workload. Which…
- Problem: Map each word of a 10⁶-word document to its frequency, then print words in alphabetical order. Which choice gives O(n log n) with…