Longest Good Sequence
Reported by candidates from Walmart's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Walmart asked this in February 2024 and it's a dynamic programming problem disguised as a sequence optimization question. You're looking at a DP state where you track the longest valid subsequence meeting some constraint. The trick is figuring out what makes a sequence 'good' and how prior elements inform the next choice. StealthCoder will read the constraint and scaffold the recurrence while you focus on implementation.
Pattern and pitfall
The pattern here is DP with memoization or tabulation, where you build up the longest valid sequence by deciding at each position whether to include that element. The gotcha: 'good' likely means adjacent or selected elements satisfy a condition (divisibility, difference bounds, or parity). You'll iterate through candidates, track the best ending at each position, then return the maximum. Common miss: thinking greedy works here. It doesn't. You need to explore all valid extensions from every prior state. That's where DP shines and where blanking gets expensive. StealthCoder catches the state definition immediately.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Longest Good Sequence 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest increasing subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Walmart's OA.
Walmart reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Good Sequence FAQ
What does 'good' mean in the sequence constraint?+
The problem text isn't shown, but 'good' typically means consecutive selected elements or adjacent positions must satisfy a math condition: divisibility, GCD, difference, or parity rule. Read the constraint twice before coding.
Is greedy fast enough here?+
No. Greedy fails because picking the largest or smallest locally valid element blocks better global solutions. DP explores all paths and memoizes, so recurrence always wins.
How do I structure the DP state?+
Usually dp[i] = longest good sequence ending at index i. Recur backwards: for each j less than i, check if arr[j] and arr[i] satisfy the 'good' rule, then dp[i] = max(dp[j]) + 1.
Will O(n-squared) time pass?+
Walmart OAs typically allow n up to 1000-5000, so O(n-squared) DP passes. If n is huge, think hash-table or greedy fallback, but standard approach is nested loops with memoization.
What if I blank on the recurrence live?+
Draw the state on paper: what info do you need to make a decision at position i? That's your DP state. Build the table row by row. StealthCoder will confirm the pattern and give you the transition logic instantly.