Solve array and list algorithms
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates proficiency in array and list algorithms, including ordering and counting techniques for relative element ranking, merging multiple sorted inputs, and algorithmic design for a package locker retrieval operation, testing data structure manipulation and retrieval logic.
Constraints
- 0 <= n <= 200000, where n is len(nums)
- -10^9 <= nums[i] <= 10^9
- Return a list of length n
- Aim for O(n log n) time; O(n) or O(n + U) space (U = number of distinct values) is acceptable
Hints
- Process elements from right to left while maintaining counts of seen values.
- Use coordinate compression to map values into a dense range.
- Maintain a Binary Indexed Tree (Fenwick Tree) to get prefix sums and update counts in O(log U).
- Alternatively, a modified merge sort can count smaller-on-right during merging.