Design O(1) cache and moving average
Company: Atlassian
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in data structures and streaming algorithms by combining an O(1) cache design with an eviction policy and a fixed-window moving-average for value streams, measuring competency in time/space complexity, stateful in-memory data handling, and algorithmic correctness.
O(1) LRU Cache Operations
Constraints
- Capacity is fixed
Examples
Input: (2, [['put', 1, 1], ['put', 2, 2], ['get', 1], ['put', 3, 3], ['get', 2]])
Expected Output: [1, -1]
Input: (0, [['put', 1, 1], ['get', 1]])
Expected Output: [-1]
Hints
- Use a map plus recency order.
Moving Window Average
Constraints
- K > 0 in normal inputs
Examples
Input: (3, [1, 10, 3, 5])
Expected Output: [1.0, 5.5, 4.666666666666667, 6.0]
Input: (1, [4, 7])
Expected Output: [4.0, 7.0]
Input: (5, [])
Expected Output: []
Hints
- Maintain a queue and running sum.