Frontend School

Optimal Resource Allocation for Batched Jobs

DSA
medium

Optimal Resource Allocation for Batched Jobs

Medium
Sorting
Searching
Arrays
Prefix Sum
Binary Search

Overview

Nvidia GPUs are powerful, but efficient resource allocation is crucial for maximizing throughput in data centers. Imagine you're managing a cluster of GPU nodes, each with a specific memory capacity. You have a queue of compute jobs, each requiring a certain amount of memory. To maximize the number of jobs run on any given node, it's a common strategy to prioritize smaller jobs. This problem asks you to determine, for various GPU node capacities, the maximum number of jobs that can be simultaneously allocated.

What you need to do / Task

Implement the allocateJobs function. Given an array of memory requirements for all available jobs and an array of memory capacities for various GPU nodes, your task is to return an array where each element corresponds to a GPU node's capacity and indicates the maximum number of jobs that can be allocated to it. Assume that jobs are always allocated optimally: by picking jobs with the smallest memory requirements first until the GPU node's capacity is reached or no more jobs can fit.

Input & output

allocateJobs(jobMemoryRequirements: number[], gpuCapacities: number[]): number[]

  • jobMemoryRequirements: An array of positive integers, where jobMemoryRequirements[i] represents the memory (in MB) required by the i-th job.
  • gpuCapacities: An array of positive integers, where gpuCapacities[j] represents the total available memory (in MB) on the j-th GPU node.

The function should return an array of integers results, where results[j] is the maximum number of jobs that can be allocated to the GPU node with capacity gpuCapacities[j].

Clarifications

  • Empty inputs: The input arrays jobMemoryRequirements and gpuCapacities will not be empty.
  • Large values: Memory requirements and capacities can be quite large. Consider how to handle sums that might exceed standard 32-bit integer limits.
  • Job order: The original order of jobMemoryRequirements does not imply any priority. You should determine the optimal allocation strategy yourself.
  • Job reuse: Each job is distinct. Once a job is conceptually "allocated" to determine the count for a specific GPU capacity, it is available again for other GPU capacities. This means you are essentially answering independent queries for each gpuCapacities[j].

Examples

1

Example 1

Input:

jobMemoryRequirements = [20, 10, 30, 15], gpuCapacities = [30, 60]

Output:

[2, 3]

Note:

  1. Sorted job requirements: [10, 15, 20, 30].
  2. Prefix sums: [10, 25, 45, 75].
  3. For capacity 30: Jobs [10, 15] sum to 25. Maximum 2 jobs.
  4. For capacity 60: Jobs [10, 15, 20] sum to 45. Maximum 3 jobs.
2

Example 2

Input:

jobMemoryRequirements = [5, 10, 15], gpuCapacities = [4, 5, 20, 30]

Output:

[0, 1, 2, 3]

Note:

  1. Sorted job requirements: [5, 10, 15].
  2. Prefix sums: [5, 15, 30].
  3. For capacity 4: No jobs fit. 0 jobs.
  4. For capacity 5: Job [5] fits. 1 job.
  5. For capacity 20: Jobs [5, 10] fit (sum 15). 2 jobs.
  6. For capacity 30: Jobs [5, 10, 15] fit (sum 30). 3 jobs.
3

Example 3

Input:

jobMemoryRequirements = [1000000000, 1000000000, 1000000000], gpuCapacities = [1, 2500000000]

Output:

[0, 2]

Note:

  1. Sorted job requirements: [10^9, 10^9, 10^9].
  2. Prefix sums: [10^9, 210^9, 310^9].
  3. For capacity 1: No jobs fit. 0 jobs.
  4. For capacity 2.510^9: Jobs [10^9, 10^9] fit (sum 210^9). 2 jobs. This example highlights the need to handle large numbers for prefix sums correctly.

Constraints

Constraints & assumptions

  • 1 <= jobMemoryRequirements.length <= 10^5
  • 1 <= gpuCapacities.length <= 10^5
  • 1 <= jobMemoryRequirements[i] <= 10^9
  • 1 <= gpuCapacities[j] <= 10^15
  • All memory requirements and capacities are positive integers.

Out of scope

  • Error handling for invalid input types (e.g., non-numeric values, negative values).
  • Actual simulation of job allocation or scheduling; only the count of allocatable jobs is required.

What interviewers evaluate

  • Efficiency of Sorting: How effectively the initial sorting of job requirements is performed.
  • Optimal Search Strategy: The choice and correct implementation of a search algorithm (e.g., binary search) to find the maximum number of jobs for each GPU capacity.
  • Data Type Handling: Correctly managing potentially large sum values to avoid overflow issues, especially in languages with fixed-size integers.
  • Overall Time and Space Complexity: The solution should demonstrate an understanding of optimal algorithmic complexity (e.g., O(N log N + M log N) time) and reasonable space complexity for the given constraints.

Loading editor...