Interview questions · Ixana
Schedule batched register operations optimally
This question evaluates scheduling and ordering under timing constraints, read-after-write hazards, intra-batch dependency handling, and the ability to parse and execute register read/write operations.
Problem
You are given a text file that describes batches of register operations (reads and writes) on integer-indexed registers. Your task is to schedule and execute these operations using provided functions, while respecting timing constraints and (for Level 2) intra-batch dependencies.
You may reorder operations within a batch, but must obey all constraints described below.
Implement the function:
def execute_operations_from_file(file_path: str):
...
You are provided with the following primitives (assume they are already implemented):
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 implementation of execute_operations_from_file must read the file, compute a valid schedule, and invoke read_reg / write_reg in that order.
Timing model and global constraints
Use a logical time measured in milliseconds (ms). You do not need to wait in real time; just ensure your computed schedule respects these rules:
-
Single operation at a time At any logical time, at most one operation (read or write) may be executing.
-
Minimum gap between operations Treat each operation as taking negligible time, but 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 at timet + 1or later.
- If one operation starts at time
-
Read-after-write hazard After a write to register
rstarts at timet, no read of registerrmay start until timet + 10or later.- That is, between a write to register
rand any subsequent read ofr, there must be at least 10 ms.
- That is, between a write to register
Your goal is to produce a valid schedule that respects all of the above rules. Within a batch, you may choose any ordering that leads to a valid schedule. (You may try to minimize the total completion time of all operations, but correctness—i.e., respecting constraints—is the primary requirement.)
Input format
The input is a plain text file. Each non-empty line is one of the following forms:
-
Write operation
<reg_index> w <value><reg_index>: an integer register index (e.g.,0,1,2, ...).w: the literal characterw(write).<value>: an integer value to be written.
Example:
2 w 100means: write the value
100to register2. -
Read operation (Level 1)
<reg_index> r<reg_index>: an integer register index.r: the literal characterr(read).
Example:
5 rmeans: read from register
5. -
Batch separator
por
P(case-insensitive: treat
pandPthe same).Each
pline ends the current batch and starts a new batch. The file therefore consists of one or more batches of operations, separated byplines.
Batch semantics
- Operations in different batches must be strictly sequential:
- No operation in batch
k+1may start before all operations in batchkhave finished, according to the logical time schedule.
- No operation in batch
- Operations within the same batch may be reordered arbitrarily, as long as:
- All global timing constraints (single operation, 1 ms gap, 10 ms read-after-write) are respected.
- All Level 2 dependency constraints (if used) are respected.
- It is guaranteed that within a single batch, no register index appears in more than one operation:
- i.e., in any batch, there is never more than one operation involving the same
<reg_index>.
- i.e., in any batch, there is never more than one operation involving the same
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.- Count only operation lines, not
p/Plines. - Line numbering 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.
- Count only operation lines, not
Dependency rule:
A read operation written as <reg_index> r <dependent_line_number> must not be scheduled before the operation on the referenced line has executed. In other words, for that pair of operations, your schedule must respect the given precedence constraint in addition to all global timing constraints.
All previous rules (single operation at a time, 1 ms gap, 10 ms read-after-write, batch ordering) still apply.
Sample input
Example input file (showing multiple batches):
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 describes 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:
- the operation type (read or write),
- the register index,
- the value (for writes),
- 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; it is sufficient that the logical schedule you compute respects all constraints.
- Invoke
-
Return value
execute_operations_from_filedoes not need to return any value; its effect is the sequence of calls toread_regandwrite_regexecuted in an order that satisfies all constraints above.
Community answers
Nothing here yet.
Join in on CSWork →