Coding questions · Waymo
Does a Seven-Segment LED Digit Display Read the Same Rotated 180 Degrees?
Decide whether a row of digits on a seven-segment LED display reads the same after the whole display is rotated by 180 degrees. Tests modeling each digit as lit segments, mapping segments under rotation, and comparing the reversed display.
An LED display shows a row of digits from 0 to 9, one digit per seven-segment cell. Write a function that decides whether the display reads exactly the same after the whole display is rotated by 180 degrees.
Function Signature
def looks_same_after_rotation(digits: list[int]) -> bool:
Rules
- Each cell has seven segments:
a(top),b(upper right),c(lower right),d(bottom),e(lower left),f(upper left) andg(middle). - The digits light these segments:
| Digit | Lit segments |
|---|---|
| 0 | a, b, c, d, e, f |
| 1 | b, c |
| 2 | a, b, d, e, g |
| 3 | a, b, c, d, g |
| 4 | b, c, f, g |
| 5 | a, c, d, f, g |
| 6 | a, c, d, e, f, g |
| 7 | a, b, c |
| 8 | a, b, c, d, e, f, g |
| 9 | a, b, c, d, f, g |
- Rotating the display by 180 degrees reverses the left-to-right order of the cells, and inside every cell it moves segment
atod,dtoa,btoe,etob,ctof,ftoc, and leavesgin place. - Return
Trueif and only if, after the rotation, every cell shows exactly the same set of lit segments as the cell in that position showed before. A rotated cell whose segment pattern is not one of the ten digits above can never match.
Constraints
1 <= len(digits) <= 10^50 <= digits[i] <= 9
Examples
Example 1
- Input:
digits = [6, 0, 9] - Output:
True - Explanation: After rotation the cells appear in reverse order, and each rotated cell shows: 9 becomes 6, 0 stays 0, 6 becomes 9. The display reads 6, 0, 9 again.
Example 2
- Input:
digits = [8, 1, 8] - Output:
False - Explanation: The 1 lights segments
bandc; rotated, it lightseandf, which is a bar on the left side of the cell and not the pattern for 1.
Example 3
- Input:
digits = [2, 5] - Output:
False - Explanation: Each of 2 and 5 looks like itself after rotation, but the cell order is reversed, so the display reads 5, 2.
Upside-Down Seven-Segment Display
An LED display shows a row of digits from 0 to 9, one digit per seven-segment cell. Decide whether the display reads exactly the same after the whole display is rotated by 180 degrees.
Implement looks_same_after_rotation(digits), where digits lists the cells from left to right.
Segments. Each cell has seven segments: a (top), b (upper right), c (lower right), d (bottom), e (lower left), f (upper left) and g (middle). The digits light these segments:
| Digit | Lit segments |
|---|---|
| 0 | a, b, c, d, e, f |
| 1 | b, c |
| 2 | a, b, d, e, g |
| 3 | a, b, c, d, g |
| 4 | b, c, f, g |
| 5 | a, c, d, f, g |
| 6 | a, c, d, e, f, g |
| 7 | a, b, c |
| 8 | a, b, c, d, e, f, g |
| 9 | a, b, c, d, f, g |
Rotation. Rotating the display by 180 degrees reverses the left-to-right order of the cells, and inside every cell it moves segment a to d, d to a, b to e, e to b, c to f, f to c, and leaves g in place.
Output. Return True (true in JavaScript, Java and C++) if and only if, after the rotation, every cell shows exactly the same set of lit segments as the cell in that position showed before. Otherwise return False. A rotated cell whose segment pattern is not one of the ten digits above can never match. The answer is a single boolean, so every input has exactly one correct output.
Example 1
- Input:
digits = [6, 0, 9] - Output:
True - Explanation: After rotation the cells appear in reverse order, and each rotated cell shows: 9 becomes 6, 0 stays 0, 6 becomes 9. The display reads 6, 0, 9 again.
Example 2
- Input:
digits = [8, 1, 8] - Output:
False - Explanation: The 1 lights segments
bandc; rotated, it lightseandf, which is a bar on the left side of the cell and not the pattern for 1.
Example 3
- Input:
digits = [2, 5] - Output:
False - Explanation: Each of 2 and 5 looks like itself after rotation, but the cell order is reversed, so the display reads 5, 2.
Constraints
1 <= len(digits) <= 10^50 <= digits[i] <= 9- Every value fits in a 32-bit signed integer; no sums or products are needed.
Hints
- Apply the segment map to each of the ten digit patterns once and look up which digit, if any, the rotated pattern spells. A small table of ten entries is all you need at run time.
- After the rotation, the cell at position i shows the rotated version of the cell that was originally at position n - 1 - i.
- Compare each position with its rotated partner in a single pass; any cell whose rotated pattern is not a digit, or is the wrong digit, makes the answer false.
Approach
First turn the segment rules into a digit-level table. Applying the map a<->d, b<->e, c<->f, g fixed to each digit's lit segments gives: 0 -> 0, 2 -> 2, 5 -> 5, 8 -> 8 (the pattern is symmetric under the half-turn), 6 -> 9 and 9 -> 6 (they swap), while 1, 3, 4 and 7 turn into patterns that are not any digit (for example 1 becomes a bar on the left side). Mark those with -1. Because the half-turn also reverses the cell order, the rotated display shows rotated(digits[n - 1 - i]) at position i. The display reads the same exactly when rotated(digits[n - 1 - i]) == digits[i] for every i; a -1 never equals a real digit, so non-digit patterns fail automatically. The reference checks every position in one pass and stops at the first mismatch. Two common traps: treating 1 as self-rotating (on this display it becomes a bar on the left side of the cell), and rejecting 2 and 5, which really do map to themselves under this segment map.
- Time complexity: O(n)
- Space complexity: O(1)