Solve classic backend coding problems
Company: Chime
Role: Backend Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Solve the following coding problems.
### 1. Find the missing number from a concatenated range
You are given an integer `n` and a string `s`.
The string was formed by taking every integer from `1` to `n`, removing exactly one number, converting the remaining numbers to decimal strings, and concatenating them together with no separators.
Return the missing number.
There are two versions:
- **Part 1:** the numbers were concatenated in increasing numeric order.
- Example: `n = 5`, `s = "1234"` → return `5`.
- **Part 2:** the numbers may appear in any order before concatenation, still with no separators.
- Example: for `n = 13`, a valid string could represent a shuffled sequence such as `13, 10, 9, 2, 3, ...` with one number missing.
Assume the input is valid and exactly one number from `1..n` is missing.
### 2. Determine the status of a square board game
Implement a function that takes:
- a board size `size`, where the board is `size x size` and `3 <= size <= 9`
- a list of action strings `moves`
Return one of:
- `"in progress"`
- `"player 1 is the winner"`
- `"player 2 is the winner"`
Assume every action is valid and well-formed.
#### Board rules
Each square may be empty or owned by one player, and it stores a number of pieces.
#### Turn structure
Players alternate turns. A turn consists of:
1. **Place** exactly once.
2. **Topple** zero or more times.
The input is a flat list of actions, not a list of turns. The first place action belongs to player 1. After each place action, subsequent topple actions belong to that same player until the next place action, which starts the other player's turn.
#### Action encoding
- `"pRC"` means place one piece at row `R`, column `C`.
- `"tRCd"` means topple the square at row `R`, column `C` in direction `d`, where `d` is one of:
- `u` = up
- `r` = right
- `d` = down
- `l` = left
#### Place action
A player may place a piece on:
- an empty square, or
- a square already owned by that player
If the square is empty, the player claims it and places one piece there.
If the square already belongs to that player, increment its piece count by one.
#### Topple action
A player may topple a square they own that contains at least 2 pieces.
- Pick up all pieces from that square.
- Move in the chosen direction, placing one piece per square on consecutive cells.
- Pieces that move beyond the board are lost.
- If a placed piece lands on an opponent-owned square, that square is captured: ownership changes to the current player, and all pieces on that square now belong to the current player.
#### Game end
The game ends as soon as one player has no pieces remaining anywhere on the board.
Examples:
- `moves = ["p10", "p12", "p10", "t10r"]` → `"player 1 is the winner"`
- `moves = ["p00", "p22", "p02", "p22", "t22u", "t02l"]` → `"player 2 is the winner"`
### 3. Generate word sequences for T9 input
A phone keypad uses this mapping:
- `2 -> abc`
- `3 -> def`
- `4 -> ghi`
- `5 -> jkl`
- `6 -> mno`
- `7 -> pqrs`
- `8 -> tuv`
- `9 -> wxyz`
Implement a function with inputs:
- `input_digits`: an array of digits, each from `2` to `9`, length up to `25`
- `valid_words`: an array of up to `50` lowercase English words
Return a 2D array containing **all** word sequences whose T9 encodings concatenate exactly to `input_digits`.
Each inner array represents one valid translation.
- Example: `input_digits = [2,2,8]`, `valid_words = ["act", "bat", "cat", "acd", "test"]` → `[["act"], ["bat"], ["cat"]]`
- Example: `input_digits = [7,6,6,3,8,4,6,3]`, `valid_words = ["some", "time", "rome", "sometime", "so", "me"]` → valid outputs include `[["rome", "time"], ["so", "me", "time"], ["some", "time"], ["sometime"]]`
Any output order is acceptable unless otherwise specified.
Quick Answer: This multi-part question evaluates algorithmic problem solving and implementation skills, including string parsing and segmentation, combinatorial reasoning for unordered concatenations, stateful simulation of game rules, and mapping/backtracking for keypad word generation.
Part 1: Missing Number in Ordered Concatenation
You are given an integer n and a string s. The string was formed by concatenating every integer from 1 to n in increasing order, except that exactly one number was omitted. Return the missing number.
Example: if n = 5 and s = '1234', the missing number is 5.
Because the numbers appear in sorted order, you should use that structure while scanning the string.
Constraints
- 1 <= n <= 100000
- s is formed from the numbers 1 to n in strictly increasing order with exactly one number missing
- Input is guaranteed to have exactly one valid answer
Examples
Input: (5, '1234')
Expected Output: 5
Explanation: The ordered string contains 1, 2, 3, 4, so 5 is missing.
Input: (9, '12456789')
Expected Output: 3
Explanation: The scan fails when expecting '3'.
Input: (13, '123456789111213')
Expected Output: 10
Explanation: After 9, the next expected token is '10', but the string continues with '11'.
Input: (1, '')
Expected Output: 1
Explanation: Edge case: the only number is missing, so the string is empty.
Hints
- Walk through the expected numbers from 1 upward and compare each number's string form against the current position in s.
- The first expected number that does not match the current prefix is the answer.
Part 2: Missing Number in Shuffled Concatenation
You are given an integer n and a string s. The string was formed by concatenating every integer from 1 to n exactly once, except that one number is missing. Unlike Part 1, the numbers were concatenated in arbitrary order, and there are no separators.
Return the missing number.
Example: if n = 5 and s = '1523', the numbers present are 1, 5, 2, 3, so the missing number is 4.
You may assume the input always has exactly one valid interpretation.
Constraints
- 1 <= n <= 20
- s is a concatenation of all numbers from 1 to n except one missing number
- Numbers are written without leading zeros
- Input is guaranteed to have exactly one valid answer
Examples
Input: (5, '1523')
Expected Output: 4
Explanation: The present numbers are 1, 5, 2, and 3.
Input: (9, '58321476')
Expected Output: 9
Explanation: All one-digit numbers except 9 appear.
Input: (13, '131211987654321')
Expected Output: 10
Explanation: A valid split is 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1.
Input: (20, '20191817161513121110987654321')
Expected Output: 14
Explanation: The shuffled string contains every number from 1 to 20 except 14.
Input: (1, '')
Expected Output: 1
Explanation: Edge case: the only number is missing.
Hints
- Because the order is arbitrary and numbers have different lengths, think about backtracking over possible splits.
- Use a set or bitmask to remember which numbers have already been used.
Part 3: Determine the Board Game Status
Implement a function that simulates a two-player board game and returns the final game status.
The board is an N x N square, where 3 <= N <= 9. Moves are given as strings.
Rules:
- Players alternate only when a place move is encountered.
- A place move has form 'prc', meaning place one piece at row r, column c.
- A player may place only on an empty square or one they already own.
- A topple move has form 'trcd', where d is one of 'u', 'r', 'd', 'l'.
- To topple, the player picks up all pieces from a square they own that has at least 2 pieces, then drops them one by one in the chosen direction starting from the next square.
- If a dropped piece lands on an opponent-owned square, all pieces on that square are captured and the square becomes owned by the current player.
- Pieces that move off the board are lost.
Return one of:
- 'in progress'
- 'player 1 is the winner'
- 'player 2 is the winner'
Assume all moves are valid and well formed.
Constraints
- 3 <= size <= 9
- Each move is valid and well formed
- Coordinates are zero-based single digits because size <= 9
- Topple moves belong to the same player as the most recent place move
Examples
Input: (3, ['p10', 'p12', 'p10', 't10r'])
Expected Output: 'player 1 is the winner'
Explanation: Player 1 topples from (1,0), captures player 2's square at (1,2), and leaves player 2 with no pieces.
Input: (3, ['p00', 'p22', 'p02', 'p22', 't22u', 't02l'])
Expected Output: 'player 2 is the winner'
Explanation: Player 2 captures (0,2), then topples again to capture (0,0).
Input: (3, [])
Expected Output: 'in progress'
Explanation: Edge case: no moves have been played.
Input: (3, ['p00', 'p11', 'p00', 't00d'])
Expected Output: 'in progress'
Explanation: After the topple, both players still have pieces on the board.
Input: (3, ['p00', 'p22', 'p00', 't00l'])
Expected Output: 'player 2 is the winner'
Explanation: Player 1 topples both pieces off the board and is left with none.
Hints
- Track both the owner and piece count for each occupied square.
- During a topple, when a piece lands on an opponent square, ownership flips and all existing pieces there become the current player's pieces.
Part 4: T9 Input Method Word Combinations
Implement a T9 input method.
You are given:
- input_digits: a list of digits from 2 to 9
- valid_words: a dictionary of valid lowercase English words
A word matches a digit sequence if each letter maps to the standard T9 keypad digit:
- 2: abc
- 3: def
- 4: ghi
- 5: jkl
- 6: mno
- 7: pqrs
- 8: tuv
- 9: wxyz
Return all possible word combinations whose encoded digits concatenate to exactly match input_digits.
Each result should be a list of words. Words may be reused multiple times. Return the full list of combinations sorted lexicographically.
For an empty input_digits list, return [[]], representing the one valid empty decomposition.
Constraints
- 0 <= len(input_digits) <= 25
- Each digit is between 2 and 9
- 1 <= len(valid_words) <= 50
- All words consist of lowercase English letters
- Words may be reused multiple times
Examples
Input: ([2, 2, 8], ['act', 'bat', 'cat', 'acd', 'test'])
Expected Output: [['act'], ['bat'], ['cat']]
Explanation: Only act, bat, and cat map to 228.
Input: ([7, 6, 6, 3, 8, 4, 6, 3], ['some', 'time', 'rome', 'sometime', 'so', 'me'])
Expected Output: [['rome', 'time'], ['so', 'me', 'time'], ['some', 'time'], ['sometime']]
Explanation: All returned combinations encode to 76638463.
Input: ([], ['a', 'b'])
Expected Output: [[]]
Explanation: Edge case: there is exactly one way to decode an empty digit sequence, by choosing no words.
Input: ([9, 9], ['a', 'b'])
Expected Output: []
Explanation: Neither 'a' nor 'b' maps to 9, so no combination exists.
Input: ([2, 2], ['a', 'b'])
Expected Output: [['a', 'a'], ['a', 'b'], ['b', 'a'], ['b', 'b']]
Explanation: Both 'a' and 'b' map to 2, and words may be reused.
Hints
- First convert every dictionary word into its T9 digit string.
- Then solve a word-break style problem using DFS with memoization over the digit positions.