← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jun 2026

Summary

Google SWE coding round with a digit DP problem that looks approachable until you actually read the constraints carefully. The 'Perfect Wake Number' definition has a few layers and getting all of them right under pressure is its own challenge.

Questions Asked (1)

Q1

Given an integer N (up to 10^18), count how many integers in [1, N] are 'Perfect Wake Numbers', where a Perfect Wake Number contains no zero digit, has no repeated digits, and no digit is sandwiched between two strictly larger neighbors.

Algorithms & Data Structures
Author's notes

The third condition is the one that'll get you.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use digit DP to count numbers <= N that satisfy the constraints, since N can be up to 10^18. The DP state should track position, tightness, mask of used digits, and the last two digits to enforce the no-sandwich condition. Precompute counts for lengths less than N's length to handle leading zeros and simplify the DP.

Pro tip: Clarify the definition of 'sandwiched' with the interviewer: it typically means a digit that is strictly smaller than both its immediate neighbors. Also, mention that the no-zero and no-repeated-digits constraints drastically reduce the search space, making digit DP efficient.

1. Clarify constraints and edge cases

Confirm the definition of 'sandwiched' and whether numbers with fewer digits than N are included. Discuss edge cases like N < 10 or numbers with leading zeros.

2. Design digit DP state

Define DP state: position, tight flag, used digit mask, and last two digits. Explain how to transition by trying each possible next digit (1-9) that is unused and doesn't create a sandwich.

3. Handle variable lengths and leading zeros

Since numbers can have fewer digits than N, either run DP for each length separately or incorporate a 'started' flag to handle leading zeros. Precompute counts for all lengths up to len(N)-1.

4. Implement and optimize

Implement memoization for the DP. Note that the mask has at most 2^10 states, and last two digits are at most 10*10, so the state space is manageable. Use bitmask operations for efficiency.

5. Test and validate

Test with small N by brute force to ensure correctness. Check edge cases like N=1, N=10, and N=10^18. Discuss time complexity: O(len(N) * 2 * 2^10 * 10^2 * 10) which is about 10^6 operations.

Key Points to Mention

  • Digit DP with state compression using bitmask for used digits
  • Handling the 'tight' constraint to ensure numbers <= N
  • Enforcing the no-sandwich condition by checking the last two digits
  • Precomputing counts for lengths shorter than N to simplify DP
  • Time and space complexity analysis
  • Edge cases: N < 10, numbers with leading zeros, and the digit 0 exclusion

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.