KatInterview

Text Formatting and Dynamic Schedule Tracker

Greedy & IntervalsAuto-graded · 17 test cases
MediumAuto-gradedGreedy & Intervalsgreedyintervalsfenwick-tree

Last updated

Perform greedy text wrapping to fixed line widths alongside dynamic interval point queries with insertion and removal.

Write a function that processes two independent tasks given as a single workload: a greedy word-formatting operation and a sequence of dynamic schedule operations.

Task 1: Word Wrapping

Format a list of strings (words) into formatted lines such that:

  • Words appear in the same order as in words.
  • Adjacent words on the same line are separated by exactly one space character.
  • A line cannot exceed length w, unless a single word itself is longer than w (in which case it occupies its own line without being broken up).
  • Each line contains as many words as possible greedily. A new line is started only when adding the next word would cause the line's total length (including separating spaces) to exceed w.

Task 2: Dynamic Schedule Active Count

Process a list of operations that modify or query an active schedule of intervals marked by unique entity identifiers. The dynamic system tracks active time ranges [start, end] (inclusive).

Operations are represented as lists with one of the following formats:

  • ["add", entity_id, start, end]: Registers an interval [start, end] for entity_id. If entity_id already exists, its previous interval is overwritten. If start > end, the interval is registered but covers zero time points.
  • ["remove", entity_id]: Deletes entity_id and its associated interval. If entity_id does not exist, this operation is ignored.
  • ["get_active_count", time]: Queries how many currently registered entities have an interval [start, end] such that start <= time <= end.

The function must return a dictionary containing two keys:

  • "lines": A list of strings corresponding to the wrapped text lines.
  • "counts": A list of integers containing the results of each "get_active_count" query in the order they appear in operations.

Constraints

  • $0 \le \text{len}(words) \le 10^4$
  • $1 \le w \le 10^5$
  • Words contain alphanumeric non-space characters.
  • $0 \le \text{len}(operations) \le 10^4$
  • start, end, and query time values are integers within range $[-10^9, 10^9]$.
  • entity_id values are arbitrary strings or integers.
  • Empty lists within operations (e.g. []) should be safely ignored.

Output Format

Return a dictionary of the form: {"lines": List[str], "counts": List[int]}

Examples

Example 1

Input: wrap_text_and_waitlist(['hello', 'world', 'this', 'is', 'a', 'test'], 10, [['add', 'A', 1, 5], ['add', 'B', 3, 7], ['get_active_count', 4], ['get_active_count', 6], ['remove', 'A'], ['get_active_count', 4]])

Output: {'lines': ['hello', 'world this', 'is a test'], 'counts': [2, 1, 1]}

Example 2

Input: wrap_text_and_waitlist(['one', 'two', 'three', 'four'], 7, [])

Output: {'lines': ['one two', 'three', 'four'], 'counts': []}

Example 3

Input: wrap_text_and_waitlist([], 10, [['get_active_count', 0], ['add', 'x', -2, 2], ['get_active_count', -2], ['get_active_count', 2], ['get_active_count', 3]])

Output: {'lines': [], 'counts': [0, 1, 1, 0]}

Hints

Hint 1

For Task 1, track the total width of the current line including spaces. Add a word if it fits; otherwise, push the current line to your output list and start a new line.

Hint 2

For Task 2, notice all query times and interval endpoints are known ahead of time. Use coordinate compression to map discrete time points to array indices.

Hint 3

Use a Fenwick tree (Binary Indexed Tree) or segment tree over the compressed coordinates to efficiently support range updates (+1 on interval add, -1 on removal) and point queries.