Simulate document assignment to indexers
Problem Description
You are tasked with simulating a document processing system involving m parallel indexers (workers), numbered from 0 to m-1. Documents arrive sequentially over time and are placed in a processing queue. Each document, when assigned to an indexer, occupies that indexer exclusively for a specified duration.
Document Assignment Rules
Documents are assigned based on the following 0-based indexing rules:
- Document
iarrives atqueue_time[i]and requiresprocessing_time[i]units of processing. - A
baseindexer is determined bybase = i % m. The system first attempts to assign the document to indexerbase. - An indexer
jis considered available for a document arriving at timetif its current processing (if any) completes no later than timet. - If indexer
baseis available atqueue_time[i], documentiis assigned to indexerbase. - Otherwise, the system scans indexers in increasing order, wrapping around modulo
m:(base + 1) % m,(base + 2) % m, ...,(base + m - 1) % m. Documentiis assigned to the first indexer found that is available atqueue_time[i]. - If no indexer is available at
queue_time[i]after checking allmindexers, the document is dropped and never processed. - Once document
iis assigned to indexerjatqueue_time[i], indexerjbecomes busy untilqueue_time[i] + processing_time[i].
Inputs
You will be provided with:
- An integer
m: The total number of indexers. - An integer array
queue_timeof lengthn:queue_time[i]represents the arrival time of documenti. This array is strictly increasing (queue_time[i] < queue_time[i+1]). - An integer array
processing_timeof lengthn:processing_time[i]is the duration required to process documenti.
Assume n = len(queue_time) = len(processing_time).
Outputs
Your algorithm should compute the following:
Step 1
- The total number of documents that were successfully processed (i.e., not dropped).
- The indexer ID that processed the most documents. If multiple indexers are tied for the maximum, any one of them is an acceptable return.
Step 2
For a given integer k (where 1 \le k \le m):
- The set of the top
kindexers that processed the largest number of documents. If there are ties, any order or any valid choice among tied indexers is acceptable. - The percentage of all documents (including those dropped) that were processed by these top
kindexers, expressed as a value between 0% and 100%.
Requirements
- Describe the algorithm to perform the simulation and compute all required outputs.
- Aim for an efficient solution in terms of time and space complexity.
Example
Input:
m = 3queue_time = [1, 2, 3, 7]processing_time = [5, 4, 3, 2]- Suppose
k = 2for Step 2.
Processing simulation:
- Document 0 arrives at time 1,
base = 0. All indexers are free. Assign to indexer 0, busy until1 + 5 = 6. - Document 1 arrives at time 2,
base = 1. Indexer 1 is free. Assign to indexer 1, busy until2 + 4 = 6. - Document 2 arrives at time 3,
base = 2. Indexer 2 is free. Assign to indexer 2, busy until3 + 3 = 6. - Document 3 arrives at time 7,
base = 0. Indexer 0 is free (available from time 6). Assign to indexer 0, busy until7 + 2 = 9.
All documents are successfully processed. Indexer 0 processed 2 documents; indexers 1 and 2 each processed 1 document.
Output:
- Documents processed successfully:
4 - Indexer that processed most documents:
0 - Top 2 indexers (any valid order with ties): e.g.,
[0, 1]or[0, 2] - Percentage of documents processed by these 2 indexers:
75%(3 out of 4 documents)
AI interview prep
Core dilemma
Efficiently simulating dynamic resource allocation with specific priority and wrap-around rules, while optimizing for performance metrics in a potentially large-scale system.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Simulate document assignment to indexers
Problem Description
You are tasked with simulating a document processing system involving m parallel indexers (workers), numbered from 0 to m-1. Documents arrive sequentially over time and are placed in a processing queue. Each document, when assigned to an indexer, occupies that indexer exclusively for a specified duration.
Document Assignment Rules
Documents are assigned based on the following 0-based indexing rules:
- Document
iarrives atqueue_time[i]and requiresprocessing_time[i]units of processing. - A
baseindexer is determined bybase = i % m. The system first attempts to assign the document to indexerbase. - An indexer
jis considered available for a document arriving at timetif its current processing (if any) completes no later than timet. - If indexer
baseis available atqueue_time[i], documentiis assigned to indexerbase. - Otherwise, the system scans indexers in increasing order, wrapping around modulo
m:(base + 1) % m,(base + 2) % m, ...,(base + m - 1) % m. Documentiis assigned to the first indexer found that is available atqueue_time[i]. - If no indexer is available at
queue_time[i]after checking allmindexers, the document is dropped and never processed. - Once document
iis assigned to indexerjatqueue_time[i], indexerjbecomes busy untilqueue_time[i] + processing_time[i].
Inputs
You will be provided with:
- An integer
m: The total number of indexers. - An integer array
queue_timeof lengthn:queue_time[i]represents the arrival time of documenti. This array is strictly increasing (queue_time[i] < queue_time[i+1]). - An integer array
processing_timeof lengthn:processing_time[i]is the duration required to process documenti.
Assume n = len(queue_time) = len(processing_time).
Outputs
Your algorithm should compute the following:
Step 1
- The total number of documents that were successfully processed (i.e., not dropped).
- The indexer ID that processed the most documents. If multiple indexers are tied for the maximum, any one of them is an acceptable return.
Step 2
For a given integer k (where 1 \le k \le m):
- The set of the top
kindexers that processed the largest number of documents. If there are ties, any order or any valid choice among tied indexers is acceptable. - The percentage of all documents (including those dropped) that were processed by these top
kindexers, expressed as a value between 0% and 100%.
Requirements
- Describe the algorithm to perform the simulation and compute all required outputs.
- Aim for an efficient solution in terms of time and space complexity.
Example
Input:
m = 3queue_time = [1, 2, 3, 7]processing_time = [5, 4, 3, 2]- Suppose
k = 2for Step 2.
Processing simulation:
- Document 0 arrives at time 1,
base = 0. All indexers are free. Assign to indexer 0, busy until1 + 5 = 6. - Document 1 arrives at time 2,
base = 1. Indexer 1 is free. Assign to indexer 1, busy until2 + 4 = 6. - Document 2 arrives at time 3,
base = 2. Indexer 2 is free. Assign to indexer 2, busy until3 + 3 = 6. - Document 3 arrives at time 7,
base = 0. Indexer 0 is free (available from time 6). Assign to indexer 0, busy until7 + 2 = 9.
All documents are successfully processed. Indexer 0 processed 2 documents; indexers 1 and 2 each processed 1 document.
Output:
- Documents processed successfully:
4 - Indexer that processed most documents:
0 - Top 2 indexers (any valid order with ties): e.g.,
[0, 1]or[0, 2] - Percentage of documents processed by these 2 indexers:
75%(3 out of 4 documents)