Schedule batched register operations optimally
Problem: Optimal Scheduling of Batched Register Operations
You are tasked with implementing a function execute_operations_from_file(file_path: str) that reads a text file describing batches of register operations (reads and writes) on integer-indexed registers. Your primary goal is to schedule and execute these operations using provided primitive functions, while strictly adhering to a set of timing and dependency constraints.
You are allowed to reorder operations within a batch to achieve a valid schedule, but operations across different batches must be processed strictly sequentially.
Provided Primitives
Assume the following functions are already implemented and available for your use:
def read_reg(register_index: int) -> int:
"""Return the current value of the register with the given index."""
...
def write_reg(register_index: int, value: int) -> None:
"""Write `value` into the register with the given index."""
...
Your execute_operations_from_file implementation must parse the input file, compute a valid schedule, and then invoke read_reg / write_reg according to that schedule.
Timing Model and Global Constraints
All timing is measured in logical milliseconds (ms). You do not need to introduce real-time delays; simply ensure your computed schedule respects these rules:
- Single Operation at a Time: At any given logical time, a maximum of one operation (read or write) may be executing.
- Minimum Gap Between Operations: Each operation is considered to take negligible time. However, the start times of any two consecutive operations must be at least 1 ms apart. If one operation starts at time
t, the next operation may start att + 1or later. - Read-After-Write Hazard: After a write to register
rstarts at timet, no read of registerrmay start until timet + 10or later. This means there must be at least 10 ms between a write to registerrand any subsequent read ofr.
Your primary objective is to produce a valid schedule that respects all these rules. While minimizing the total completion time of all operations is desirable, correctness (adhering to all constraints) is the fundamental requirement.
Input Format
The input is a plain text file. Each non-empty line represents either an operation or a batch separator:
-
Write Operation:
<reg_index> w <value><reg_index>: An integer register index (e.g.,0,1,2, ...).w: The literal characterw(indicating a write).<value>: An integer value to be written. Example:2 w 100means: write the value100to register2.
-
Read Operation (Level 1):
<reg_index> r<reg_index>: An integer register index.r: The literal characterr(indicating a read). Example:5 rmeans: read from register5.
-
Batch Separator:
por
P(case-insensitive). Each
porPline ends the current batch and starts a new batch. The file consists of one or more batches of operations, separated by these lines.
Batch Semantics
- Strict Sequential Batching: Operations in batch
k+1must not start before all operations in batchkhave finished, according to the logical time schedule. - Intra-Batch Reordering: Operations within the same batch may be reordered arbitrarily, provided all global timing constraints (single operation, 1 ms gap, 10 ms read-after-write) and Level 2 dependency constraints (if applicable) are respected.
- No Duplicate Registers per Batch: It is guaranteed that within a single batch, no register index appears in more than one operation (i.e., no two operations in the same batch involve the same
<reg_index>).
Level 2: Intra-Batch Read Dependencies
For Level 2, some read operations within a batch may have an additional dependency on another operation line within the same batch.
Such a dependent read operation has the format:
<reg_index> r <dependent_line_number>
Where:
<reg_index>: An integer register index to be read.r: The literal characterr.<dependent_line_number>: A 1-based line index within the same batch, referring to another operation line in that batch. This count includes only operation lines, notp/Plines, and restarts at 1 for each batch. The referenced line can be either a read or a write operation. It is guaranteed that<dependent_line_number>is valid and refers to a line in the same batch as this read.
Dependency Rule: A read operation specified as <reg_index> r <dependent_line_number> must not be scheduled before the operation on the referenced line has executed. This precedence constraint must be respected in addition to all global timing constraints.
Sample Input
2 w 100
3 w 10
4 w 10
5 r
p
4 r
1 r
2 r
p
3 w 10
p
2 w 10
3 w 8
5 r
This file demonstrates multiple batches of reads and writes, separated by lines containing p.
Requirements for execute_operations_from_file
- Parsing:
- Open and read the file at
file_path. - Split the contents into batches using
p/Plines as separators. - Within each batch, parse each operation line into an internal representation that captures: operation type (read/write), register index, value (for writes), and for Level 2, the optional dependency on another line within the same batch.
- Open and read the file at
- Scheduling:
- Using a logical time in milliseconds, compute a valid schedule that assigns a start time to each operation such that:
- Only one operation executes at any given time.
- There is at least 1 ms between the start times of consecutive operations.
- No read of a register occurs within 10 ms of a previous write to the same register.
- All operations in batch
kfinish before any operation in batchk+1begins. - For Level 2, each dependent read occurs after its referenced operation line in the same batch.
- Using a logical time in milliseconds, compute a valid schedule that assigns a start time to each operation such that:
- Execution:
- Invoke
write_regandread_regaccording to the order implied by your schedule. - You do not need to actually sleep or delay in real time; the logical schedule is sufficient.
- Invoke
- Return Value:
execute_operations_from_filedoes not need to return any value; its effect is the sequence of calls toread_regandwrite_regexecuted in a constraint-satisfying order.
AI interview prep
Core dilemma
Optimally scheduling operations with complex timing constraints (1ms gap, 10ms Read-After-Write hazard) and intra-batch dependencies, while respecting strict batch sequencing and maximizing parallelism within batches.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Schedule batched register operations optimally
Problem: Optimal Scheduling of Batched Register Operations
You are tasked with implementing a function execute_operations_from_file(file_path: str) that reads a text file describing batches of register operations (reads and writes) on integer-indexed registers. Your primary goal is to schedule and execute these operations using provided primitive functions, while strictly adhering to a set of timing and dependency constraints.
You are allowed to reorder operations within a batch to achieve a valid schedule, but operations across different batches must be processed strictly sequentially.
Provided Primitives
Assume the following functions are already implemented and available for your use:
def read_reg(register_index: int) -> int:
"""Return the current value of the register with the given index."""
...
def write_reg(register_index: int, value: int) -> None:
"""Write `value` into the register with the given index."""
...
Your execute_operations_from_file implementation must parse the input file, compute a valid schedule, and then invoke read_reg / write_reg according to that schedule.
Timing Model and Global Constraints
All timing is measured in logical milliseconds (ms). You do not need to introduce real-time delays; simply ensure your computed schedule respects these rules:
- Single Operation at a Time: At any given logical time, a maximum of one operation (read or write) may be executing.
- Minimum Gap Between Operations: Each operation is considered to take negligible time. However, the start times of any two consecutive operations must be at least 1 ms apart. If one operation starts at time
t, the next operation may start att + 1or later. - Read-After-Write Hazard: After a write to register
rstarts at timet, no read of registerrmay start until timet + 10or later. This means there must be at least 10 ms between a write to registerrand any subsequent read ofr.
Your primary objective is to produce a valid schedule that respects all these rules. While minimizing the total completion time of all operations is desirable, correctness (adhering to all constraints) is the fundamental requirement.
Input Format
The input is a plain text file. Each non-empty line represents either an operation or a batch separator:
-
Write Operation:
<reg_index> w <value><reg_index>: An integer register index (e.g.,0,1,2, ...).w: The literal characterw(indicating a write).<value>: An integer value to be written. Example:2 w 100means: write the value100to register2.
-
Read Operation (Level 1):
<reg_index> r<reg_index>: An integer register index.r: The literal characterr(indicating a read). Example:5 rmeans: read from register5.
-
Batch Separator:
por
P(case-insensitive). Each
porPline ends the current batch and starts a new batch. The file consists of one or more batches of operations, separated by these lines.
Batch Semantics
- Strict Sequential Batching: Operations in batch
k+1must not start before all operations in batchkhave finished, according to the logical time schedule. - Intra-Batch Reordering: Operations within the same batch may be reordered arbitrarily, provided all global timing constraints (single operation, 1 ms gap, 10 ms read-after-write) and Level 2 dependency constraints (if applicable) are respected.
- No Duplicate Registers per Batch: It is guaranteed that within a single batch, no register index appears in more than one operation (i.e., no two operations in the same batch involve the same
<reg_index>).
Level 2: Intra-Batch Read Dependencies
For Level 2, some read operations within a batch may have an additional dependency on another operation line within the same batch.
Such a dependent read operation has the format:
<reg_index> r <dependent_line_number>
Where:
<reg_index>: An integer register index to be read.r: The literal characterr.<dependent_line_number>: A 1-based line index within the same batch, referring to another operation line in that batch. This count includes only operation lines, notp/Plines, and restarts at 1 for each batch. The referenced line can be either a read or a write operation. It is guaranteed that<dependent_line_number>is valid and refers to a line in the same batch as this read.
Dependency Rule: A read operation specified as <reg_index> r <dependent_line_number> must not be scheduled before the operation on the referenced line has executed. This precedence constraint must be respected in addition to all global timing constraints.
Sample Input
2 w 100
3 w 10
4 w 10
5 r
p
4 r
1 r
2 r
p
3 w 10
p
2 w 10
3 w 8
5 r
This file demonstrates multiple batches of reads and writes, separated by lines containing p.
Requirements for execute_operations_from_file
- Parsing:
- Open and read the file at
file_path. - Split the contents into batches using
p/Plines as separators. - Within each batch, parse each operation line into an internal representation that captures: operation type (read/write), register index, value (for writes), and for Level 2, the optional dependency on another line within the same batch.
- Open and read the file at
- Scheduling:
- Using a logical time in milliseconds, compute a valid schedule that assigns a start time to each operation such that:
- Only one operation executes at any given time.
- There is at least 1 ms between the start times of consecutive operations.
- No read of a register occurs within 10 ms of a previous write to the same register.
- All operations in batch
kfinish before any operation in batchk+1begins. - For Level 2, each dependent read occurs after its referenced operation line in the same batch.
- Using a logical time in milliseconds, compute a valid schedule that assigns a start time to each operation such that:
- Execution:
- Invoke
write_regandread_regaccording to the order implied by your schedule. - You do not need to actually sleep or delay in real time; the logical schedule is sufficient.
- Invoke
- Return Value:
execute_operations_from_filedoes not need to return any value; its effect is the sequence of calls toread_regandwrite_regexecuted in a constraint-satisfying order.