Solve scheduling and collision problems
Company: J.P. Morgan
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of algorithm design and data structures for resource allocation and dynamic interaction problems, specifically interval overlap analysis for scheduling/CPU provisioning and one-dimensional collision simulation.
Part 1: Minimum Concurrent Resources for Intervals
Constraints
- `0 <= len(intervals) <= 200000`
- `0 <= start < end <= 10^9` for every interval
- The input list may be unsorted
Examples
Input: [[0, 30], [5, 10], [15, 20]]
Expected Output: 2
Explanation: At most two intervals are active at the same time, so two resources are enough.
Input: [[7, 10], [2, 4]]
Expected Output: 1
Explanation: The intervals do not overlap, so one resource can be reused.
Input: []
Expected Output: 0
Explanation: No intervals means no resources are needed.
Input: [[1, 2]]
Expected Output: 1
Explanation: A single interval always needs exactly one resource.
Input: [[1, 4], [4, 5], [4, 6]]
Expected Output: 2
Explanation: The interval `[1, 4]` frees its resource at time 4, then the other two intervals overlap with each other.
Hints
- If you process intervals in increasing order of start time, you only need to know which active interval ends earliest.
- A min-heap of end times lets you release finished jobs before assigning the next one.
Part 2: One-Dimensional Collision Simulation
Constraints
- `0 <= len(objects) <= 100000`
- `1 <= abs(objects[i]) <= 10^9`
- Each `objects[i]` is non-zero
Examples
Input: [5, 10, -5]
Expected Output: [5, 10]
Explanation: The `-5` collides with `10` and disappears because `10` has larger magnitude.
Input: [8, -8]
Expected Output: []
Explanation: Both objects have equal magnitude, so both disappear.
Input: [10, 2, -5]
Expected Output: [10]
Explanation: The `-5` destroys `2`, then collides with `10` and disappears.
Input: []
Expected Output: []
Explanation: An empty input stays empty.
Input: [-2, -1, 1, 2]
Expected Output: [-2, -1, 1, 2]
Explanation: These objects never move toward each other, so no collisions happen.
Hints
- A collision can only happen when a right-moving object is to the left of a left-moving object.
- Use a stack to keep survivors seen so far, and resolve collisions while the top of the stack can still meet the current object.