JPMorgan Chase Coding Questions: Three Patterns with Solved Examples
Know a solution but struggle when the input changes? Practise complement lookup, sliding windows and take-or-skip dynamic programming with exact traces and clear invariants.
KnowledgeGate Team
Exam prep & CS education

You can reproduce a coding solution, but a changed input leaves you unsure which approach to choose. The useful next step is to connect the problem's requirements to a method, then explain why each update is correct. These three original practice examples build that habit through exact traces, invariants and complexity analysis. They are preparation exercises, not verified previous JPMorgan Chase assessment questions.
What to prepare for a JPMorgan Chase coding assessment
According to the undated JPMorganChase hiring FAQ, certain opportunities may require technical assessments through online tests or live technical interviews. Use your own invitation for the format, supported languages and timing. The FAQ does not provide a pattern-frequency list; the patterns below are chosen for practice.
Required result | Practice method |
|---|---|
Two positions whose values sum to a target | Complement lookup with a hash map |
Longest contiguous substring without repetition | Sliding window |
Maximum total from non-adjacent positions | Dynamic programming |
Constraints and required output determine the method. Returning indices, for example, means you must retain positions rather than just values. An invariant is a fact maintained after every step; it explains why an algorithm keeps working as inputs change. If arrays, loops or collections need revision, start with the coding and DSA learning options.
Hash-map pattern: find two distinct positions with a target sum
Given a = [4, 7, 1, 9] and target 10, return any pair of distinct zero-based indices, or [-1, -1] if absent. The answer is [2, 3] because 1 + 9 = 10.
Brute force checks up to n(n-1)/2 pairs, which is 4 x 3 / 2 = 6 here. Instead, ask whether an earlier value equals target - a[i].
import java.util.HashMap;
// Place inside a class.
int[] twoSum(int[] a, int target) {
HashMap<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < a.length; i++) {
int needed = target - a[i];
if (seen.containsKey(needed)) {
return new int[] {seen.get(needed), i};
}
seen.put(a[i], i);
}
return new int[] {-1, -1};
}The invariant: before each lookup, every map entry comes from an earlier position.
i | Value | Complement | Lookup | Map after step |
|---|---|---|---|---|
0 | 4 | 10 - 4 = 6 | Miss |
|
1 | 7 | 10 - 7 = 3 | Miss |
|
2 | 1 | 10 - 1 = 9 | Miss |
|
3 | 9 | 10 - 9 = 1 | Hit at 2 | Return |

For [5,5], target 10, the second lookup finds index 0, returning [0,1]. For [5], no pair exists. Lookup before insertion prevents self-pairing.
Expected time is O(n); extra space is O(n). This example's arithmetic fits Java int. Larger bounds need long complements and keys, with subtraction widened before evaluation. Continue with HashMap lookup and frequency examples.
Sliding window: longest substring without repeated characters
For the ASCII string abba, return the length of the longest contiguous substring containing distinct characters. The answer is 2, from ab or ba. A substring is contiguous; a subsequence can skip positions.
Maintain a left boundary and a map of last-seen indices. Java-style pseudocode:
left = 0; best = 0
last = empty map
for right from 0 to s.length - 1:
ch = s[right]
if last contains ch:
left = max(left, last[ch] + 1)
last[ch] = right
best = max(best, right - left + 1)
return bestright | Character | left | Active window | Length | best |
|---|---|---|---|---|---|
0 | a | 0 | a | 0 - 0 + 1 = 1 | 1 |
1 | b | 0 | ab | 1 - 0 + 1 = 2 | 2 |
2 | b | 2 | b | 2 - 2 + 1 = 1 | 2 |
3 | a | 2 | ba | 3 - 2 + 1 = 2 | 2 |
The invariant is that the active window contains no repeated character. At the final a, its previous index is 0. The candidate boundary 0 + 1 = 1 lies behind left = 2, so max keeps 2.

With ASCII keys and constant-time lookups, time is O(n) and storage is O(min(n, alphabet size)). Empty input returns 0; aaaa returns 1; abcd returns 4. This implementation assumes ASCII, not full Unicode grapheme-cluster handling.
Dynamic programming: maximum sum without adjacent elements
Given non-negative integers [3, 2, 7, 10], choose non-adjacent elements to maximise their sum. An empty selection is allowed. The answer is 13, using indices [0,3]: 3 + 10 = 13. Choosing only the largest element gives 10 and misses a compatible earlier value.
Define dp[i] as the best sum from the first i elements. Start with dp[0] = 0 and dp[1] = 3.
dp[i] = max(dp[i-1], dp[i-2] + a[i-1])
dp[2] = max(3, 0 + 2) = 3
dp[3] = max(3, 3 + 7) = 10
dp[4] = max(10, 3 + 10) = 13Every valid selection either skips the current element or takes it. Skipping preserves the previous optimum; taking excludes the adjacent position and adds the current value to the optimum two positions back. These cases are exhaustive.
Retain only the two required states:
twoBack = 0; oneBack = 0
for value in a:
next = max(oneBack, twoBack + value)
twoBack = oneBack
oneBack = next
return oneBackAfter each step, oneBack holds the processed prefix's optimum, and twoBack holds the preceding prefix's optimum. Use a numeric type that holds the total.
Time is O(n) and auxiliary space is O(1) for the sum only. Reconstructing indices needs additional bookkeeping. Edge results: [] returns 0, [8] returns 8, [0,0] returns 0. Requiring at least one element with negative inputs changes initialisation and is outside this problem's contract.
Three traps that break otherwise correct ideas
Inserting first feels natural. With
[5]and target10, it falsely lets index0pair with itself. Look up before inserting to preserve distinct indices.Every seen character looks like a collision. The final
ainabbawas seen outside the current window. Usemaxso the left boundary never moves backwards.Updating DP in place looks shorter. Overwriting
twoBackfirst makes the take case read the wrong state. Computenextbefore shifting either state.
Before submission, check empty inputs, repeated values, array bounds, exact output format and permitted numeric range. A correct idea still fails if you return values when indices were requested.
Practise explaining and testing under a time limit
Try a 30-minute practice drill: five minutes to restate inputs, outputs and constraints; fifteen to implement; five to test; five to explain the invariant and complexity. This is a practice schedule, not an official test duration. Rotate the examples across sessions.
Retest with one changed input:
[2,7,11,15], target9: indices[0,1], since2 + 7 = 9.abcabcbb: length3, withabcas one longest window.[2,1,1,2]: sum4, choosing the first and last elements.
For planning your attempts, use coding-round time management.
The short version and your next practice step
A required complement suggests a hash map. A contiguous unique segment suggests a sliding window. A take-or-skip optimum suggests dynamic programming. Confirm each choice against the constraints before coding.
Solve one example again without looking, then explain its invariant aloud. If you want structured study after these exercises, the DSA Using Java course is an optional next step for building your problem-solving foundation.
Keep learning
Publicis Sapient Coding Questions: Three Patterns with Worked Solutions
Recognising code is easier than explaining why it works. Practise three patterns through Java methods, exact traces, invariants and edge cases before your next coding assessment.
Wells Fargo Coding Questions: Three Patterns with Worked Solutions
Learn to recognise three coding patterns from the task itself. Follow complete Python solutions, trace each state change, and catch mistakes involving duplicate values, repeated characters and negative numbers.
CAT VARC Reading Accuracy: A 6-Week Plan Before You Chase Attempt Speed
Stop measuring progress only by attempts. This six-week home-practice system builds defensible answers, stable accuracy and then controlled speed.
Physical Security Controls and Access Management: Three-Zone Worked Example and Exam Notes
Learn how layered physical controls work with authentication, authorization and audit. Then test four requests against a complete three-zone access policy.





