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.
Solve the following coding problems.
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:
n = 5
,
s = "1234"
→ return
5
.
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.
Implement a function that takes:
size
, where the board is
size x size
and
3 <= size <= 9
moves
Return one of:
"in progress"
"player 1 is the winner"
"player 2 is the winner"
Assume every action is valid and well-formed.
Each square may be empty or owned by one player, and it stores a number of pieces.
Players alternate turns. A turn consists of:
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.
"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
A player may place a piece on:
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.
A player may topple a square they own that contains at least 2 pieces.
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"
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.
input_digits = [2,2,8]
,
valid_words = ["act", "bat", "cat", "acd", "test"]
→
[["act"], ["bat"], ["cat"]]
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.