KatInterview

RLE Vector Dot Product and Execution Log Processor

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designarraysstackstwo-pointers

Last updated

Compress sparse sequences into run-length encodings, calculate their dot product, and analyze nested call timestamps using a call stack.

Implement a dual-task utility that performs run-length encoding operations on dense integer sequences and evaluates exclusive function execution durations from structured event traces.

Task 1: Compressed Dot Product

  1. Given two integer lists a and b of equal total length, convert each into its run-length encoded (RLE) form: consecutive identical values are grouped into pairs [value, count].
  2. Compute the dot product $\sum a[i] \cdot b[i]$ using only the generated run-length representations (without rebuilding the original uncompressed lists).

Task 2: Exclusive Call Duration

Given $n$ functions indexed $0$ through $n-1$ operating on a single-threaded system, process a chronological sequence of event log strings formatted as standard "<function_id>:<event_type>:<timestamp>".

  • event_type can be "start" or "end".
  • Functions may call other functions recursively or sequentially.
  • Duration is calculated exclusively (time spent inside child calls is excluded from the parent function's runtime).
  • A "start" log at time $t$ indicates execution begins at the start of time step $t$.
  • An "end" log at time $t$ indicates execution continues through the end of time step $t$ (inclusive).

Return a dictionary containing four keys:

  • "compressed_a": RLE form of a as a list of [value, count] pairs.
  • "compressed_b": RLE form of b as a list of [value, count] pairs.
  • "dot_product": The computed dot product integer.
  • "exclusive_times": A list of length $n$ containing the net exclusive execution units for each function ID from $0$ to $n-1$.

Constraints

  • $1 \le \text{len}(a) = \text{len}(b) \le 10^7$
  • Values in $a$ and $b$ fit in signed 32-bit integers.
  • $0 \le \text{function_id} < n \le 10^4$
  • $0 \le \text{timestamp} \le 10^9$
  • Timestamps in logs are well-formed, non-decreasing, and correctly balanced.

Examples

Example 1

Input: optimize_vectors_and_exclusive_times([1, 1, 1, 2, 2], [3, 3, 4, 4, 4], 2, ['0:start:0', '1:start:2', '1:end:5', '0:end:6'])

Output: {'compressed_a': [[1, 3], [2, 2]], 'compressed_b': [[3, 2], [4, 3]], 'dot_product': 26, 'exclusive_times': [3, 4]}

Example 2

Input: optimize_vectors_and_exclusive_times([0, 0, 0, 5, 5, -1], [2, 2, 3, 3, 3, -4], 3, ['0:start:0', '0:end:0', '1:start:2', '2:start:3', '2:end:4', '1:end:5'])

Output: {'compressed_a': [[0, 3], [5, 2], [-1, 1]], 'compressed_b': [[2, 2], [3, 3], [-4, 1]], 'dot_product': 34, 'exclusive_times': [1, 2, 2]}