KatInterview

One-Edit Word Lookup and Vertical Tree Traversal

Data Structure DesignModel answer
MediumModel answerData Structure Designhashingtriebinary-tree

Last updated

Design a data structure for single-character substitution queries, then output a binary tree's node values sorted by column, row, and left-to-right order.

Part A — Single-Substitution Word Index

Build a class with two methods:

  • build(words: List[str]) — indexes a list of words.
  • search(query: str) -> bool — returns True if and only if some indexed word can be obtained by replacing exactly one character of query.

Rules and edge cases to address:

  • Only single-character substitutions count. Insertions, deletions, and transpositions do not.
  • Words of a different length than query can never match.
  • If query itself appears in the index, that alone is not a match — it needs a distinct neighbor that differs in exactly one position.
  • Duplicate entries in words must not produce false positives.
  • Words may contain characters outside lowercase ASCII; your design should handle this without relying on a fixed alphabet.

For your answer: describe at least two candidate designs, explain which you would implement and why, state time and space complexity for both build and search, and provide working code.


Part B — Binary Tree Nodes in Column-Row Order

You are given a binary tree whose nodes each hold a single lowercase letter. Coordinates are assigned as follows:

  • The root is at (row=0, col=0).
  • A left child inherits (row+1, col−1).
  • A right child inherits (row+1, col+1).

Return a single string formed by concatenating every node's character, sorted by:

  1. Column value, ascending.
  2. Row value, ascending within each column.
  3. Left-to-right tree order for nodes that share both row and column.

Example:

        a
       / \
      b   c
     / \   \
    d   e   f

Coordinates: a(0,0), b(1,−1), c(1,1), d(2,−2), e(2,0), f(2,2).

Output: "dbaefc"

Tie-breaking example: if a tree has both root.left.right and root.right.left, they both land at (row=2, col=0). The node reached via the left subtree should come first.

For your answer: describe your traversal strategy, explain how each sort criterion is satisfied, analyze time and space complexity, and provide working code.

Hints

Hint 1

For Part A, think about what two equal-length strings share when they differ in exactly one position. Can you turn that structural relationship into a hash key that lets you skip character-by-character comparisons at query time?

Hint 2

For Part A's correctness, consider how many distinct words you actually need to store per bucket to answer any query — you do not need the full word list, just enough to distinguish 'query is the only word here' from 'another distinct word is also here'.

Hint 3

For Part B, BFS naturally visits nodes level by level and left-to-right within each level, which matches two of the three sort criteria for free. Think about what data structure lets you collect characters per column as BFS runs, so you only need to emit columns in order at the end rather than sorting all nodes.