KatInterview

Multi-Component Data Processing Challenge

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designarraysqueuessimulation

Last updated

Tests array merging from the back, queue-based task scheduling with time slices, and price-level order matching in a simulated trading book.

You will implement three independent components in a single function that returns a dictionary with three keys.

Component 1: Merge Two Sorted Arrays

You receive two sorted arrays, arr1 and arr2. The first array contains extra space at the end (filled with zeros) to accommodate all elements from the second array. Merge arr2 into arr1 so the result is sorted in ascending order.

The number of meaningful elements in arr1 equals len(arr1) - len(arr2). The remaining positions are buffer space.

Return the merged result as merged_array in the output dictionary.

Component 2: Task Scheduler with Time Slices

Implement a round-robin task scheduler that processes a sequence of operations:

  • add_task with arguments [task_id, duration]: adds a new task to the queue
  • execute_next with arguments [time_slice]: runs the next task in queue for up to time_slice units

When executing a task:

  • Reduce its remaining duration by the actual time run (minimum of time_slice and current remaining duration)
  • Record the result as [task_id, remaining_duration] after execution
  • If the task still has remaining duration greater than zero, move it to the back of the queue
  • If the queue is empty, record None

For add_task operations, record None in the results.

Tasks execute in the order they were added (FIFO). When a task is re-queued after partial execution, it goes to the back.

Return the list of results (one per operation) as scheduler_results in the output dictionary.

Component 3: Order Book Matching

You have an order book represented as a dictionary mapping price levels to available volume: {price: volume}.

Process a list of trades, where each trade is [trade_type, price, volume]:

  • trade_type is either "BUY" or "SELL"
  • For "BUY" trades, consume volume starting from the lowest price level
  • For "SELL" trades, consume volume starting from the highest price level

For each trade:

  • Remove volume from price levels in the specified order
  • When a price level's volume reaches zero, remove that level from the book
  • If the trade volume exceeds available liquidity, consume what is available

The price field in each trade is not used for matching (trades consume all available levels in priority order).

Return the final state of the order book as order_book in the output dictionary.

Return Value

Return a dictionary with exactly three keys:

{
    "merged_array": [...],      # sorted merged array
    "scheduler_results": [...], # list of results per operation
    "order_book": {...}         # final price-to-volume mapping
}

Constraints

  • 0 ≤ len(arr2) ≤ len(arr1) ≤ 10⁴
  • 0 ≤ len(operations) ≤ 10³
  • 0 ≤ number of price levels ≤ 10³
  • 0 ≤ len(trades) ≤ 10³
  • All integers fit in 32-bit signed range
  • Array elements may be negative
  • Task IDs and durations are positive integers
  • Price levels and volumes are positive integers
  • Time slices are positive integers

Examples

Example 1

Input: process_combined_tasks([1, 3, 5, 0, 0], [2, 4], [], [], {}, {})

Output: {'merged_array': [1, 2, 3, 4, 5], 'scheduler_results': [], 'order_book': {}}

Example 2

Input: process_combined_tasks([1, 3, 5, 0, 0], [2, 4], [], [], {}, [])

Output: {'merged_array': [1, 2, 3, 4, 5], 'scheduler_results': [], 'order_book': {}}

Hints

Hint 1

For array merging, working backwards prevents overwriting unread values in the destination array.

Hint 2

Use a deque for the scheduler to get efficient removal from the front and addition to the back.

Hint 3

For order matching, sort the price levels once per trade and iterate in the appropriate direction.