KatInterview

Binary Layout Manager with Dynamic Field Insertion

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designbinary-search-treeprefix-sumdata-structure-design

Last updated

Tests ability to maintain ordered sequences with efficient prefix-sum queries and positional insertions using balanced tree structures.

You are building a system to manage the memory layout of binary objects. Each object contains an ordered sequence of fields, where every field has a name and occupies a certain number of bytes. Fields are placed one after another with no gaps, starting at byte 0.

The byte offset of each field is determined by summing the sizes of all fields that come before it in the sequence.

Example layout:

Given fields (alpha, 4), (beta, 5), (gamma, 3) in that order:

FieldSizeByte Range
alpha4[0, 4)
beta5[4, 9)
gamma3[9, 12)

Your task is to implement a function that processes a series of operations on a field layout:

  • getOffset(name): Return the starting byte offset of the field with the given name.
  • insert(name, size, position): Insert a new field at the specified position in the field sequence (0-indexed). All fields currently at this position or later shift one position toward the end, and their byte offsets increase accordingly.

Input Format

  • fields: A list of [name, size] pairs representing the initial field sequence.
  • operations: A list of operation names, each either "getOffset" or "insert".
  • arguments: A list where each element contains the arguments for the corresponding operation:
    • For "getOffset": [name]
    • For "insert": [name, size, position]

Output Format

Return a list of results, one per operation:

  • For "getOffset": the integer byte offset
  • For "insert": None (or null)

Example

Initial fields: [["foo", 3], ["bar", 6], ["baz", 2]]

Operations:

  1. getOffset("bar") → 3 (foo occupies bytes 0-2)
  2. insert("qux", 2, 1) → sequence becomes foo, qux, bar, baz
  3. getOffset("bar") → 5 (foo: 0-2, qux: 3-4, bar starts at 5)
  4. getOffset("baz") → 11 (bar ends at 10)
  5. insert("end", 4, 4) → appends to the end
  6. getOffset("end") → 13
  7. getOffset("foo") → 0

Constraints

  • 1 ≤ number of initial fields ≤ 10⁵
  • 1 ≤ total operations ≤ 10⁵
  • 1 ≤ field size ≤ 10⁴
  • All field names are unique non-empty strings
  • getOffset is only called on fields that exist
  • For insert operations: 0 ≤ position ≤ current field count
  • All byte offsets fit in a 64-bit signed integer

Examples

Example 1

Input: binary_layout_manager([['foo', 3], ['bar', 6], ['baz', 2]], ['getOffset', 'insert', 'getOffset', 'getOffset', 'insert', 'getOffset', 'getOffset'], [['bar'], ['qux', 2, 1], ['bar'], ['baz'], ['end', 4, 4], ['end'], ['foo']])

Output: [3, None, 5, 11, None, 13, 0]

Hints

Hint 1

Think about what getOffset really computes: it's a prefix sum over the field sequence. How can you maintain prefix sums when the sequence changes?

Hint 2

Inserting in the middle of a sequence is expensive with arrays. Consider a tree structure where in-order traversal gives the field order.

Hint 3

An implicit treap with subtree aggregates can support O(log n) insertion by position and O(log n) prefix-sum queries by walking up parent pointers.