Process Tasks Using Servers - Problem

Imagine you're managing a data center where multiple servers need to process incoming tasks efficiently. You have n servers with different processing capabilities (weights) and m tasks that arrive sequentially over time.

The Challenge: You need to assign each task to the most suitable available server following these rules:

  • ๐Ÿ• Task j arrives at second j and joins a queue
  • โš–๏ธ Always assign tasks to the lightest available server (smallest weight)
  • ๐Ÿ”ข If weights are equal, choose the server with the smallest index
  • โณ A server processing a task becomes unavailable for tasks[j] seconds
  • ๐Ÿšซ If no servers are free, tasks wait in queue until one becomes available

Goal: Return an array where ans[j] is the index of the server that processes task j.

Input: Two arrays - servers[i] (server weights) and tasks[j] (task durations)

Output: Array of server indices showing which server processes each task

Input & Output

example_1.py โ€” Basic Assignment
$ Input: servers = [3,3,2], tasks = [1,2,3,2,1,2]
โ€บ Output: [2,2,0,2,1,2]
๐Ÿ’ก Note: Tasks arrive at seconds 0,1,2,3,4,5. Server 2 (weight=2) is lightest and handles most tasks. When busy, tasks wait or use other servers based on availability and weight.
example_2.py โ€” All Servers Busy
$ Input: servers = [5,1,4,3,2], tasks = [2,1,2,4,5,2,1]
โ€บ Output: [1,4,1,4,1,3,2]
๐Ÿ’ก Note: Server 1 (weight=1) is preferred but becomes busy. Tasks are assigned to next best available servers or wait for preferred servers to become free.
example_3.py โ€” Single Server
$ Input: servers = [10], tasks = [1,1,1,1]
โ€บ Output: [0,0,0,0]
๐Ÿ’ก Note: Only one server available, so all tasks must be assigned to server 0, with tasks queuing when the server is busy.

Constraints

  • n == servers.length
  • m == tasks.length
  • 1 โ‰ค n, m โ‰ค 2 ร— 105
  • 1 โ‰ค servers[i], tasks[j] โ‰ค 2 ร— 105
  • All server weights and task durations are positive integers

Visualization

Tap to expand
Data Center Task Scheduling SystemTask QueueT1T2T3T4Incoming TasksServer FarmServer 0Weight: 3AvailableServer 1Weight: 5BusyServer 2Weight: 2AvailableServer 3Weight: 4AvailableAssignment Logic1. Check available servers2. Find minimum weight3. Break ties by index4. If none free, waitTask โ†’ Server 2Lightest server selectedKey Insights๐Ÿฅ Like hospital triage: assign patients to most suitable available doctorโšก Priority heaps enable O(log n) server selection and status updates๐ŸŽฏ Two heaps: available (by weight/index) + busy (by completion time)
Understanding the Visualization
1
Tasks Arrive
Tasks enter the system at regular intervals and join a processing queue
2
Find Best Server
System identifies the lightest available server (or waits for one to become free)
3
Assign & Process
Task is assigned to the selected server and begins processing
4
Update Status
Server status is updated and will become available again after task completion
Key Takeaway
๐ŸŽฏ Key Insight: Use two priority heaps to efficiently manage server availability and selection, achieving optimal O((m+n) ร— log n) performance
Asked in
Amazon 28 Google 15 Microsoft 12 Meta 8
31.8K Views
High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen