Reported September 2026
Amazondynamic programming

Longest Increasing Subsequence With Bounded Adjacent Difference

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The Amazon OA reported in September 2026 hides its trick in one clause: consecutive picks must differ by at most k, and the array can hit 200,000 elements. That kills the textbook O(n^2) LIS. This is dynamic programming with a data structure bolted on. dp[i] is the best subsequence length ending at arr[i], and you need the max dp over values in [arr[i]-k, arr[i]-1] seen earlier. If you blank on the structure, StealthCoder can run invisibly during the live OA as a safety net. Know the shape before you sit down.

The problem

Given a non-empty integer array arr and a non-negative integer k, return the maximum length of a subsequence that satisfies all of the following:
The selected values are strictly increasing.
The difference between every pair of consecutive selected values is at most k.
The selected values preserve their relative order in arr.
A subsequence may delete any number of elements without changing the order of the remaining elements.

Function
longestBoundedIncreasingSubsequence(arr: int[], k: int) → int

Examples
Example 1
arr = [7,1,4,5,8,8,10,6,7,7,7,8]
k = 4
return = 6
One longest valid subsequence is [1,4,5,6,7,8]. It preserves input order, every adjacent difference is at most 4, and its length is 6.
Example 2
arr = [3,1,2,6,10,11,4,5]
k = 3
return = 4
The subsequence [1,2,4,5] is strictly increasing, preserves input order, and has adjacent differences 1, 2, and 1.
Example 3
arr = [5,4,3,2,1]
k = 2
return = 1
No two values form a strictly increasing pair in subsequence order, so every valid longest subsequence contains one value.

Constraints
1 <= arr.length <= 2 * 10^5
-10^9 <= arr[i] <= 10^9
0 <= k <= 2 * 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

Define dp[i] as the longest valid subsequence ending at index i. The transition is dp[i] = 1 + max(dp[j]) over earlier j where arr[j] is in [arr[i]-k, arr[i]-1]. Scanning all j is O(n^2), which times out at 2 * 10^5. Fix it by coordinate-compressing values and keeping a segment tree (or Fenwick variant for range max) indexed by value. For each element, query the max over the range, add 1, then point-update at arr[i]. That's O(n log n). Pitfalls: the upper bound is arr[i]-1 because strictly increasing rules out equal values, so duplicates like the 8,8 and 7,7,7 in Example 1 can't chain. Also k can reach 2 * 10^9, so use 64-bit or careful bounds when computing arr[i]-k. Query before updating so an element never extends itself. If the tree logic slips under pressure, StealthCoder is the hedge on the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Longest Increasing Subsequence With Bounded Adjacent Difference 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Increasing Subsequence With Bounded Adjacent Difference FAQ

What's the trick in this Amazon bounded LIS problem?+

Replace the inner loop of the O(n^2) DP with a range-max query. Index a segment tree by compressed value, query [arr[i]-k, arr[i]-1] for the best earlier length, add 1, and write it back at arr[i]. That gives O(n log n).

Why does the naive LIS DP fail here?+

With n up to 2 * 10^5, checking every earlier element is about 4 * 10^10 operations in the worst case. The OA expects you to notice the constraint and move to a logarithmic query structure instead of nested loops.

How do I handle duplicates and equal values?+

The sequence must be strictly increasing, so query only up to arr[i]-1. Equal values never extend each other. In Example 1 the repeated 7s and 8s can each start or end a chain, but they can't sit next to each other in one.

Do I need a segment tree or will a Fenwick tree work?+

A segment tree is the safer choice for range max with point updates. A Fenwick tree handles prefix max easily, but a bounded window [arr[i]-k, arr[i]-1] needs a true range query, so a segment tree is cleaner.

How do I prepare for this in 48 hours?+

Write the segment tree for range max once from memory, then solve classic LIS with it. Then add the k bound and coordinate compression. Test on the three examples, especially the decreasing array that returns 1, and on k = 0, which should return 1.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it