Same Substring Within Budget
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The JP Morgan OA reported in September 2026 hinges on one structure: a variable-size window over a cost array. Same Substring Within Budget gives you two strings and a budget K, and you want the longest contiguous stretch you can afford to convert. It's a sliding-window problem dressed up as string work. If you spot that in the first minute, the rest is bookkeeping. If you blank, StealthCoder is the invisible safety net running during the live OA, reading the problem and handing you a working solution. The pattern is simple. The traps are in the details.
The problem
You are given two equal-length lowercase strings s and t, and an integer budget K. Changing s[i] into t[i] costs the absolute difference between their lowercase-letter positions. Choose one contiguous substring of s and change every character in it to the corresponding character of t. Return the maximum possible substring length whose total change cost is at most K. A zero-length substring is allowed. Function sameSubstring(s: String, t: String, K: int) → int Examples Example 1 s = "uaccd" t = "gbbeg" K = 4 return = 3 The position costs are [14,1,1,2,3]. The range from index 1 through index 3 costs 1 + 1 + 2 = 4, so length 3 is possible. No longer range fits the budget. Example 2 s = "abcd" t = "bcdf" K = 3 return = 3 The first three positions each cost 1, so abc can become bcd for total cost 3. Adding the fourth position would exceed the budget. Constraints 1 <= s.length = t.length <= 2 * 10^5 0 <= K <= 10^6 s and t contain lowercase English letters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Turn the strings into a cost array first: cost[i] = |s[i] - t[i]|. Now the task is the longest subarray with sum at most K. All costs are non-negative, and that's what makes a sliding window valid. Expand the right pointer, add its cost to a running sum, and while the sum exceeds K, move the left pointer forward and subtract. Track the max of right - left + 1. That's O(n) time and O(1) extra space if you compute costs inline. The common pitfall is using a prefix sum with nested loops, which is O(n^2) and dies at length 2 * 10^5. Another miss is forgetting K = 0, where only zero-cost positions count and the answer can be 0. If the window logic slips under pressure, StealthCoder can hedge you during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Same Substring Within Budget 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as get equal substrings within budget. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Same Substring Within Budget FAQ
What's the trick in Same Substring Within Budget?+
Convert each position to its absolute letter difference, then find the longest subarray with sum at most K. Costs are never negative, so a two-pointer sliding window works. Grow right, shrink left while over budget, record the best length.
How hard is this JP Morgan OA question really?+
Medium at most. Once you see it as a subarray-sum-under-budget problem, it's about ten lines. The difficulty is recognizing the pattern and avoiding an O(n^2) brute force with a 2 * 10^5 input size.
Why does sliding window work here and not for every sum problem?+
Because every cost is zero or positive. Adding an element never lowers the sum, and removing one never raises it. That monotonic behavior lets the left pointer only move forward. Negative values would break it.
What edge cases should I test before submitting?+
Test K = 0 with no matching characters, where the answer is 0. Test identical strings, where every cost is 0 and the answer is the full length. Test a single character. Test a case where one huge cost sits in the middle and splits the window.
How do I prepare for this in 48 hours?+
Write the fixed template for the longest subarray with sum at most K from memory. Practice it on a couple of variants, then code this one end to end. Focus on the shrink loop condition and updating the max after shrinking, not before.