Get Longest Match
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's 'Get Longest Match' hit the OA circuit in January 2024, and it's a pattern-matching problem that catches candidates off-guard. You've got a string and you need to find the longest substring or subsequence that matches some condition, but the exact constraint isn't handed to you on a silver platter. This is a dynamic-programming play, and the trap is overthinking the state. StealthCoder can confirm the DP recurrence the moment you see the full problem text on screen.
Pattern and pitfall
The core trick: set up a DP table where dp[i][j] or dp[i] tracks the longest valid match ending at position i (or spanning i to j). The pitfall is confusing substring (contiguous) with subsequence (order matters, not contiguous). Amazon loves this ambiguity. You'll likely need to iterate through the string and maintain state for either a sliding window or a two-pointer approach baked into the DP. Common mistake: forgetting to reset or carry forward the match length correctly between states. The pattern is sound if you recognize early that you're building up a best answer incrementally. Use StealthCoder to double-check your recurrence before you code; a wrong transition burns 20 minutes.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Get Longest Match cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest common subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Longest Match FAQ
Is this substring or subsequence?+
The problem title doesn't say. Assume substring (contiguous) first, but be ready to pivot to subsequence if your DP breaks. Check the examples on screen. Amazon will give you a clue in the constraints or the expected output shape.
How hard is the DP transition?+
Not brutal. Usually a 1D or 2D DP where you compare characters and decide: extend the match, reset it, or skip. Time is O(n) or O(n^2) depending on whether you need nested loops. Space can be optimized to O(1) or O(n).
What's the most common wrong approach?+
Greedy matching or naive brute force. Candidates try to match character-by-character without storing state. DP forces you to revisit past decisions, which is exactly why it works here.
How much time should I spend coding this?+
Aim for 15-20 minutes if you spot the DP pattern. If you're stuck at 10 minutes, re-read the problem and look for the recurrence. If still blank, that's when you want a safety net.
Do I need to optimize space after getting the DP working?+
Not unless the input is huge or the problem asks for it. Get the logic right and passing test cases first. Amazon values correctness and clarity over micro-optimizations in a live OA.