← Questions

Split keys into an index and a pattern and check the board

0
DatabricksCodingLast asked Aug 2024

A coding interview question asked in Databricks's online assessment, last asked Aug 2024. It tests strings and simulation.

You get a board, a string of digits, and a list of keys, each also a string of digits. Split every key at every possible position into a non-empty prefix and a non-empty suffix. Read the prefix as a 0-based index into the board (leading zeros allowed) and the suffix as a pattern. For each split, report the pattern if the board contains it starting at that index, else "not found".

Interface

Unless you agree on a different interface with the interviewer, assume:

  • check_patterns(board: str, keys: list[str]) -> list[str] — process keys in order, and each key's splits from the shortest prefix to the longest.

Example

check_patterns("2311453915", ["0211", "639"])
->  ["not found", "11", "not found", "39", "not found"]
# 0211: 0|211 (board[0:] = "231..."), 02|11 (board[2:4] = "11"), 021|1 (index 21 is past the end)
# 639:  6|39 (board[6:8] = "39"), 63|9 (index 63 is past the end)

Constraints

  • The board has up to 1,000 digits.
  • An index past the end of the board, or a pattern that runs past the end, is "not found".

Clarifying questions

  • Can an index be larger than the board? (Yes, then it is not found.)
  • Do leading zeros in the prefix change the index (02 is 2)?
  • Should keys of length 1 (no valid split) produce nothing?

What a strong answer covers

  • A loop over split positions with int(prefix) and a slice comparison; bounds checked before comparing.
  • Cost: O(total key length × pattern length) with plain comparison; with many keys, a suffix array or hashing of the board for O(1) substring checks.
  • Tests: index past the end, a pattern that overruns the board, leading zeros, a one-digit key.

Follow-ups

  • Return, for each key, the first split that matches.
  • The board is very long and there are millions of keys. Use rolling hashes.
  • Allow the pattern to match anywhere at or after the index.

Where it's asked

What it tests

strings · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding