← 返回博客列表
Google

Google Interview: RPG Combo Chain (O(N) Solution)

2025-08-13

🎮 Background: RPG Combo Chain

In a real-time Google SDE interview assistance session, we encountered a deceptively simple but tricky problem—a Subsequence + DP variant wrapped in an "RPG game combo system".

Key points:

With oavoservice's assistance, the candidate successfully jumped from a naive approach to the optimal solution and provided a space-friendly "path reconstruction" scheme in the follow-up, securing the next round for Google SDE Intern.

💬 Problem Requirements (Scenario Adapted)

Scenario: You are playing an RPG game. Each level has a difficulty value, given in an array order. Goal: You want to achieve the longest "Combo Chain". Rule: For every level you pick, the next level's difficulty must be exactly 1 greater than the previous one. You can skip levels, but must maintain original order. Task: Return the maximum length of such a Combo Chain.

Example

Input:  [10, 5, 11, 6, 7, 12, 8]
Output: 4
Explanation: Longest valid chain: 5 -> 6 -> 7 -> 8
# 10 -> 11 -> 12 has length 3, shorter.

🧠 Thought Process: From Clarification to State Definition

In the first 2 minutes, oavoservice guided the candidate through key Clarifications:

Many candidates would apply the LIS template here:

Complexity: (O(N^2)). This might pass LeetCode, but in a Google interview, it signals "not sharp enough".

oavoservice's Key Hint: Compress to O(N) using "Fixed Difference 1"

We prompted a higher-level abstraction on the shared screen:

"This is a subsequence problem with fixed difference 1. Use a Hash Map to track 'longest chain ending at value'."

State definition:

When traversing to difficulty x:

  1. Check if x - 1 exists in our map.
  2. If yes, extend that chain: dp[x] = dp[x - 1] + 1.
  3. If no, start a new chain: dp[x] = 1.

One pass + Hash Map achieves O(N).

💻 Code Skeleton (Python)

def longest_combo(levels):
    dp = {}          # dp[x] = max length ending at val x
    max_combo = 0

    for level in levels:
        prev_length = dp.get(level - 1, 0)
        dp[level] = prev_length + 1
        max_combo = max(max_combo, dp[level])

    return max_combo

With oavoservice's hint, the candidate got it right immediately, skipping the N^2 detour. The interviewer nodded in approval.

🔁 Follow-up: How to Return the Specific Sequence?

The interviewer asked:

"If we need to return the specific sequence (e.g., [5, 6, 7, 8]) instead of just the length, what do we do?"

Common reaction:

Result:

oavoservice's Minimalist Reconstruction Idea

Our hint:

"Don't store the list. Just remember the end value of the max chain. The rest can be deduced."

Approach:

  1. When updating max_combo, record the corresponding end_val.

    if dp[level] > max_combo:
        max_combo = dp[level]
        end_val = level
    
  2. Knowing:

    • Max length L = max_combo
    • Ending difficulty end_val

    The chain must be:

    [end_val - L + 1, ..., end_val - 1, end_val]
    
  3. Generate and reverse.

Candidate explanation:

"Since the step is strictly +1, once I know the max length and the ending value, I can reconstruct the sequence in O(L) just by subtracting 1 repeatedly."

Interviewer's comment: "Simple and efficient."

🎓 Interview Takeaways


Need Interview Assistance? Contact oavoservice

Need real interview questions? Contact WeChat Coding0201 immediately to get real questions.