Complete the Run: Find the Gap in a Five-Card Sequence
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests ability to handle encoded card ranks, Ace-as-low/high semantics, ambiguous endpoints, and missing-element logic in a fixed consecutive sequence.
Complete the Run: Find the Gap in a Five-Card Sequence
You receive 4 distinct characters, each representing a playing card rank. Together they form an incomplete 5-card run — five consecutive ranks — with exactly one rank absent. Your task is to identify and return that missing rank.
Rank Encoding
Every rank is represented as a single character using this scheme:
| Character | Rank |
|---|---|
'0' | Ace |
'2' – '9' | 2 through 9 |
'T' | 10 |
'J' | Jack |
'Q' | Queen |
'K' | King |
What Counts as a Run
A run is any five consecutive ranks from the ordering below:
A – 2 – 3 – 4 – 5 – 6 – 7 – 8 – 9 – 10 – J – Q – K – A
Ace may appear at the bottom of a run (A-2-3-4-5) or at the top (10-J-Q-K-A). Wrap-around sequences such as Q-K-A-2-3 are not valid runs.
This gives exactly 10 valid runs:
| # | Run |
|---|---|
| 1 | A-2-3-4-5 |
| 2 | 2-3-4-5-6 |
| 3 | 3-4-5-6-7 |
| 4 | 4-5-6-7-8 |
| 5 | 5-6-7-8-9 |
| 6 | 6-7-8-9-10 |
| 7 | 7-8-9-10-J |
| 8 | 8-9-10-J-Q |
| 9 | 9-10-J-Q-K |
| 10 | 10-J-Q-K-A |
Tie-Breaking Rule
Some sets of four cards fit inside more than one valid run (for example, 3-4-5-6 sits inside both run 2 and run 3). When this occurs, choose the lowest-numbered run from the table above. In other words, prefer the run whose lowest card has the smallest rank value.
Return Value
Return the missing rank as a single character using the same encoding described above (return '0' if the missing card is an Ace).
Raise ValueError if the four input cards cannot all be found in any single valid run.
Constraints
- The input list always contains exactly 4 elements.
- All 4 characters are distinct.
- Each character belongs to
{'0','2','3','4','5','6','7','8','9','T','J','Q','K'}. - Input order is arbitrary; the ranks may appear in any sequence.
- When multiple runs are valid for a given input, return the missing card from the lowest valid run.
Examples
Example 1
Input: complete_the_run(['3', '4', '5', '6'])
Output: '2'
Example 2
Input: complete_the_run(['0', '2', '3', '5'])
Output: '4'
Hints
Hint 1
There are only 10 possible valid runs in total. Consider writing them all out explicitly rather than computing them on the fly.
Hint 2
Convert the input list to a set, then check each run in order from lowest to highest. The first run that fully contains all four input cards is your answer.
Hint 3
When you find a matching run, the missing card is simply the one element in that run's set that is not present in the input set. The ordering of runs from lowest to highest automatically handles tie-breaking.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Insight
There are only 10 valid five-card runs in a 13-rank deck with Ace-low and Ace-high (but no wrap-around). Because the search space is constant and tiny, the cleanest correct strategy is to enumerate all 10 runs explicitly and check each one.
For each run, ask: "Are all four input cards members of this run?" If yes, the missing card is whichever element of that run is absent from the input. Return it immediately.
Iterating from run 1 (lowest) to run 10 (highest) and returning on the first match naturally enforces the tie-breaking rule: when an input is ambiguous, the lowest valid run wins.
Why This Is Correct
- Coverage. Every legitimate run of five consecutive ranks (Ace low or high, no wrap-around) appears in the table. Nothing is missed.
- Membership test.
present.issubset(run)is true precisely when all four distinct input cards live in that run. Because a run has exactly five distinct ranks, there is always exactly one rank left over — the missing card. - Ace handling at no extra cost.
'0'appears in the first run (Ace low) and the last run (Ace high). No explicit value-switching is needed; the table captures both roles directly. - Tie-breaking. The list is sorted lowest-to-highest, so the loop's first match is always the lowest applicable run, satisfying the stated rule.
- Invalid inputs. If no run contains all four cards, the loop exhausts without returning and a
ValueErroris raised.
Complexity
- Time: O(1) — at most 10 runs, each with 5 cards, and the input always has 4 cards. The total work is bounded by a small constant regardless of input.
- Space: O(1) — a fixed table of 50 characters plus a set of 4 input values.
Common Pitfall: Numeric Mapping
A tempting alternative is to convert every rank to a number, sort, and look for a gap. This works in straightforward cases but introduces several tricky sub-problems:
- Ace must be tried as both 1 and 14, and the correct interpretation has to be chosen.
- When all four cards are already consecutive, the missing card is at one end, requiring a "pick lower end" rule and boundary checks.
- The numeric answer must then be mapped back to the encoded character, and both 1 and 14 must map to
'0'.
Each of these steps is an opportunity for an off-by-one error or a missed edge case. The enumeration approach avoids every one of them: the table is easy to read, easy to verify against the 10 known runs, and handles all edge cases without any arithmetic.
Reference Solution
from typing import List
def complete_the_run(ranks: List[str]) -> str:
# All 10 valid runs listed from lowest to highest.
# This ordering also serves as the tie-breaking rule: the first match wins.
runs = [
["0", "2", "3", "4", "5"],
["2", "3", "4", "5", "6"],
["3", "4", "5", "6", "7"],
["4", "5", "6", "7", "8"],
["5", "6", "7", "8", "9"],
["6", "7", "8", "9", "T"],
["7", "8", "9", "T", "J"],
["8", "9", "T", "J", "Q"],
["9", "T", "J", "Q", "K"],
["T", "J", "Q", "K", "0"],
]
present = set(ranks)
for run in runs:
if present.issubset(run):
missing = set(run) - present
if len(missing) == 1:
return next(iter(missing))
raise ValueError("The ranks do not form four cards from any valid run")