Implement array merge, round-robin scheduler, and trading simulator
You are given three independent coding prompts. For each prompt, clearly define your function/class interfaces, handle edge cases, and analyze time/space complexity.
1) Merge sorted arrays in-place
Given two non-decreasing integer arrays:
nums1of lengthm + n, where the firstmelements are valid and the lastnelements are placeholders.nums2of lengthn.
Merge nums2 into nums1 so that nums1 becomes a single sorted array.
Inputs
nums1: int[],m: intnums2: int[],n: int
Output
- Modify
nums1in-place.
Constraints (typical interview scale)
0 ≤ m, n ≤ 2e5- Values fit in 32-bit signed integer.
Follow-up
- How would you merge three sorted arrays (
A,B,C) into one sorted array? - How would you merge them while also removing duplicates (i.e., output strictly increasing values)?
2) Implement a round-robin task scheduler
Implement a round-robin CPU/task scheduler with a fixed time quantum q.
Each task has:
id(string/int)arrivalTime(non-negative integer)burstTime(positive integer total runtime required)
Scheduling rules:
- Tasks enter the ready queue at their
arrivalTime. - The scheduler repeatedly takes the next task from the head of the queue and runs it for
min(q, remainingTime). - If the task finishes, it leaves the system.
- If not finished, it goes to the back of the queue.
- Tasks that arrive while another task is running should be enqueued as soon as they arrive (but do not preempt the current time slice).
Output Return the execution trace as a list of segments like:
(taskId, startTime, endTime)
Include idle time segments if the CPU is idle.
3) Simulate a simplified stock trading system
Design and implement a simplified in-memory trading simulator that processes a time-ordered stream of events.
Each event is one of:
DEPOSIT amountBUY symbol qty priceSELL symbol qty price
Rules:
- Start with zero cash and zero positions.
- A
BUYdecreases cash byqty * priceand increases position forsymbolbyqty. - A
SELLincreases cash byqty * priceand decreases position byqty. - Reject any
BUYthat would make cash negative. - Reject any
SELLthat would make the position for that symbol negative.
Output After processing all events, return:
- final cash balance
- final positions per symbol
- list of rejected events (or their indices)
Follow-ups (optional, if time allows)
- Add average cost basis per symbol.
- Add realized P&L using FIFO or average cost.
AI interview prep
Core dilemma
Effectively managing state and processing time-ordered events across diverse problem domains (algorithms, OS simulation, financial simulation) while demonstrating robust, efficient, and correct implementations under various constraints.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement array merge, round-robin scheduler, and trading simulator
You are given three independent coding prompts. For each prompt, clearly define your function/class interfaces, handle edge cases, and analyze time/space complexity.
1) Merge sorted arrays in-place
Given two non-decreasing integer arrays:
nums1of lengthm + n, where the firstmelements are valid and the lastnelements are placeholders.nums2of lengthn.
Merge nums2 into nums1 so that nums1 becomes a single sorted array.
Inputs
nums1: int[],m: intnums2: int[],n: int
Output
- Modify
nums1in-place.
Constraints (typical interview scale)
0 ≤ m, n ≤ 2e5- Values fit in 32-bit signed integer.
Follow-up
- How would you merge three sorted arrays (
A,B,C) into one sorted array? - How would you merge them while also removing duplicates (i.e., output strictly increasing values)?
2) Implement a round-robin task scheduler
Implement a round-robin CPU/task scheduler with a fixed time quantum q.
Each task has:
id(string/int)arrivalTime(non-negative integer)burstTime(positive integer total runtime required)
Scheduling rules:
- Tasks enter the ready queue at their
arrivalTime. - The scheduler repeatedly takes the next task from the head of the queue and runs it for
min(q, remainingTime). - If the task finishes, it leaves the system.
- If not finished, it goes to the back of the queue.
- Tasks that arrive while another task is running should be enqueued as soon as they arrive (but do not preempt the current time slice).
Output Return the execution trace as a list of segments like:
(taskId, startTime, endTime)
Include idle time segments if the CPU is idle.
3) Simulate a simplified stock trading system
Design and implement a simplified in-memory trading simulator that processes a time-ordered stream of events.
Each event is one of:
DEPOSIT amountBUY symbol qty priceSELL symbol qty price
Rules:
- Start with zero cash and zero positions.
- A
BUYdecreases cash byqty * priceand increases position forsymbolbyqty. - A
SELLincreases cash byqty * priceand decreases position byqty. - Reject any
BUYthat would make cash negative. - Reject any
SELLthat would make the position for that symbol negative.
Output After processing all events, return:
- final cash balance
- final positions per symbol
- list of rejected events (or their indices)
Follow-ups (optional, if time allows)
- Add average cost basis per symbol.
- Add realized P&L using FIFO or average cost.