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:
-
nums1
of length
m + n
, where the first
m
elements are valid and the last
n
elements are placeholders.
-
nums2
of length
n
.
Merge nums2 into nums1 so that nums1 becomes a single sorted array.
Inputs
-
nums1: int[]
,
m: int
-
nums2: int[]
,
n: int
Output
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 amount
-
BUY symbol qty price
-
SELL symbol qty price
Rules:
-
Start with zero cash and zero positions.
-
A
BUY
decreases cash by
qty * price
and increases position for
symbol
by
qty
.
-
A
SELL
increases cash by
qty * price
and decreases position by
qty
.
-
Reject any
BUY
that would make cash negative.
-
Reject any
SELL
that 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.