Merge intervals and design rating APIs
Company: Atlassian
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
### Problem 1: Merge overlapping intervals
You are given a list of closed intervals `intervals`, where `intervals[i] = [start_i, end_i]` and `start_i <= end_i`.
**Task:** Merge all overlapping intervals and return a new list of non-overlapping intervals that cover the same ranges.
**Input:**
- `intervals`: an array/list of `[start, end]` pairs (not necessarily sorted)
**Output:**
- A list of merged, non-overlapping intervals (order can be by increasing start)
**Follow-up:**
- Support inserting one additional interval `newInterval` into the existing set and return the merged result efficiently.
---
### Problem 2: Implement agent-rating APIs with ordering by average
Design and implement a data structure that supports the following APIs for rating “agents” by name.
- `rateAgent(name, score)`: record a new rating for the agent `name`.
- `name` is a string identifier.
- `score` is an integer in `[1, 5]`.
- `getTopAgents()`: return a list of agent names sorted by their **current average score** in **descending** order.
**Details / requirements:**
- Each agent can receive many ratings over time.
- The average for an agent is computed over all ratings recorded so far for that agent.
- `getTopAgents()` may be called many times; aim for an efficient approach across many operations.
- Define a deterministic tie-breaker for equal averages (e.g., by name ascending).
**Constraints (assume typical interview scale):**
- Up to ~`10^5` total API calls.
- Many repeated `getTopAgents()` calls should not require excessive recomputation each time.
Quick Answer: This question evaluates algorithmic problem-solving for interval manipulation and stateful data-structure design for maintaining and ordering agent averages, probing skills in sorting/merging, incremental updates, complexity analysis, and deterministic tie-breaking within the Coding & Algorithms domain.
Merge Overlapping Intervals
Merge closed intervals and optionally insert one new interval before merging.
Constraints
- Each interval has start <= end
Examples
Input: ([[1, 3], [2, 6], [8, 10], [15, 18]], None)
Expected Output: [[1, 6], [8, 10], [15, 18]]
Explanation: Overlapping intervals are coalesced.
Input: ([[1, 2], [3, 4]], [2, 3])
Expected Output: [[1, 4]]
Explanation: Touching closed intervals merge.
Input: ([], [5, 7])
Expected Output: [[5, 7]]
Explanation: Insert into empty set.
Input: ([[5, 5], [1, 2]], None)
Expected Output: [[1, 2], [5, 5]]
Explanation: Unsorted input.
Hints
- Sort by start, then extend the last merged interval when ranges overlap.
Agent Ratings Ordered by Average
Process rate/top operations and return agent names sorted by descending average rating with name as the tie-breaker.
Constraints
- Scores are integers in [1, 5]
Examples
Input: ((('rate', 'alice', 5), ('rate', 'bob', 4), ('top',), ('rate', 'bob', 5), ('top',)),)
Expected Output: [['alice', 'bob'], ['alice', 'bob']]
Explanation: Top queries reflect updated averages.
Input: ((('top',),),)
Expected Output: [[]]
Explanation: No agents yet.
Input: ((('rate', 'zoe', 4), ('rate', 'amy', 4), ('top',)),)
Expected Output: [['amy', 'zoe']]
Explanation: Ties sort by name.
Hints
- Keep running sum/count per agent and sort only when a top query observes dirty data.