KatInterview

Three Mini Simulations: Case Folding, Aligned Allocator, Run Counter

Data Structure DesignAuto-graded · 25 test cases
MediumAuto-gradedData Structure Designsimulationstringshash-map

Last updated

Solve three independent simulations: case-insensitive adjacent changes, an 8-aligned memory allocator, and online counting of same-color runs on a line.

You are given four batches of independent test scenarios. Process each batch and return all the results together in one dictionary.

Task A: Case-blind neighbor changes

For each string in texts, count the indices i (with 0 <= i < len(s) - 1) where s[i].lower() != s[i + 1].lower(). Strings with fewer than two characters give 0. Characters that are not letters are compared as they are.

Task B: Aligned memory allocator

The k-th entry of initial_memories is a 0/1 array describing a memory of n cells (0 = free, 1 = occupied). The k-th entry of memory_ops is the list of operations to run, in order, on that memory. Each scenario starts fresh.

Each operation is a two-element list [code, value]:

  • [0, size] (allocate). Find the smallest start index s such that s % 8 == 0, s + size <= n, and all cells s .. s+size-1 are currently free. If one exists:

    • Mark those cells occupied under a new allocation ID.
    • IDs are numbered 1, 2, 3, ... in order of successful allocations. A failed allocation does not use up an ID.
    • Output [s, id].

    If none exists, or if size < 0, output [-1, -1]. A request with size == 0 is valid: it succeeds at start 0, takes an ID, and occupies no cells.

  • [1, id] (erase). If id belongs to an allocation that is still live, free exactly the cells of that allocation and output True. Otherwise (unknown ID, or already erased) output False.

Cells that are 1 in the initial array stay occupied forever and belong to no ID. Freed cells can be reused immediately.

Task C: Counting runs of equal color

Each entry of color_updates is a separate timeline of updates [position, color], applied in order to an initially empty line. An update sets the color at integer position, either coloring a new position or overwriting an existing one.

After each update, output the number of runs. A run is a maximal group of consecutive integer positions that are all colored with the same color. Positions with no color are not part of any run and break continuity. Positions may be negative or large.

Output

Return a dictionary with exactly these keys:

  • "adjacent_counts": one integer per string in texts.
  • "allocator_results": one list per memory scenario, containing one result per operation ([start, id] pairs or booleans, as described above).
  • "segment_counts": one list per timeline in color_updates, containing the run count after each update.

Constraints

  • Any of the four input lists may be empty. The output lists have matching lengths.
  • len(initial_memories) == len(memory_ops).
  • Each memory has length 0 <= n <= 5000 and contains only 0 and 1.
  • Allocation sizes and erase IDs are integers (sizes may be 0 or negative).
  • Total number of operations and updates across all scenarios is at most 10^4.
  • Positions and colors are integers. Positions fit in 64-bit signed integers.
  • String lengths are at most 10^5.

Examples

Example 1

Input: run_three_simulations([], [[0, 0]], [[[0, 3], [0, 2], [1, 1], [1, 2]]], [[[1, 7], [2, 7], [3, 7], [2, 8], [2, 7]]])

Output: {'adjacent_counts': [], 'allocator_results': [[[-1, -1], [0, 1], True, False]], 'segment_counts': [[1, 1, 1, 3, 1]]}

Example 2

Input: run_three_simulations([], [[1, 1]], [[[0, 1], [1, 1]]], [[[0, 1], [2, 1], [1, 1]]])

Output: {'adjacent_counts': [], 'allocator_results': [[[-1, -1], False]], 'segment_counts': [[1, 2, 1]]}

Hints

Hint 1

Task A needs one pass over neighboring pairs; lowercase both characters before comparing.

Hint 2

For Task B, keep a map from live ID to the cells it owns. Try starts 0, 8, 16, ... in order and check that the whole block is free. Only increase the ID counter when an allocation succeeds.

Hint 3

For Task C, an update can only change the runs touching its own position. Remove the old point's contribution to the count, then add the new point's contribution, looking only at the neighbors at position-1 and position+1.