Autocoder

JPMorgan coding round questions with solutions

Last updated 2026-11-19

JPMorgan Chase technology assessments in India are reported to use HackerRank-style problems in the easy to medium range, with arrays, strings, hashing, binary search and some SQL, and for some programmes a numerical or logic section. The ten problems below follow that style as practice. They are not recalled questions.

Every answer has the approach, complexity, the common mistake and runnable Python and Java. For the interview rounds that follow, see JPMorgan interview questions.

StyleEasy to medium arrays, strings, hashing, SQL
Languages herePython, Java, SQL
Problems8 coding, 2 MCQ-style
LevelEasy to medium

How to approach this set

These problems reward clean, correct solutions more than clever ones. Read the constraints first: they tell you whether O(n^2) will pass. Test your code on an empty input, a single element and the largest input size before submitting.

If you have a week, do two problems a day, rewrite each solution from memory the next day, and review the platform rules in how to crack an online assessment. For the SQL problem, practise on a few tables you create yourself. The matching DSA coding round guide covers timing.

JPMorgan coding round questions with solutions: questions with worked answers

  1. 1

    Rotate an array to the right by k positions in place.

    easy

    Reduce k modulo n, then reverse the whole array, reverse the first k elements and reverse the remaining n - k. Each element moves a constant number of times: time O(n), extra space O(1). A slice-based answer is O(n) space. Mistakes: not taking k % n (so k larger than n crashes or loops) and reversing the segments in the wrong order.

    python
    def rotate(a, k):
        n = len(a)
        k %= n
        def rev(i, j):
            while i < j:
                a[i], a[j] = a[j], a[i]
                i += 1; j -= 1
        rev(0, n - 1); rev(0, k - 1); rev(k, n - 1)
    java
    class Solution {
        static void rotate(int[] a, int k) {
            int n = a.length;
            k %= n;
            rev(a, 0, n - 1); rev(a, 0, k - 1); rev(a, k, n - 1);
        }
        static void rev(int[] a, int i, int j) {
            while (i < j) { int t = a[i]; a[i++] = a[j]; a[j--] = t; }
        }
    }
  2. 2

    Group a list of words into anagram groups.

    easy

    Two words are anagrams if their sorted letters match, so use the sorted string as a hash key and collect words under it. With m words of length at most L the time is O(m L log L), and space O(m L). A counting signature (26 counts as the key) lowers it to O(m L). Mistake: using a set of characters as the key, which merges words with different letter counts, such as 'aab' and 'abb'.

    python
    from collections import defaultdict
    def group_anagrams(words):
        g = defaultdict(list)
        for w in words:
            g[''.join(sorted(w))].append(w)
        return list(g.values())
    java
    import java.util.*;
    class Solution {
        static List<List<String>> groupAnagrams(String[] words) {
            Map<String, List<String>> g = new HashMap<>();
            for (String w : words) {
                char[] c = w.toCharArray();
                Arrays.sort(c);
                g.computeIfAbsent(new String(c), x -> new ArrayList<>()).add(w);
            }
            return new ArrayList<>(g.values());
        }
    }
  3. 3

    Return the k most frequent words, ties broken alphabetically.

    medium

    Count with a hash map, then take the k best using a heap or sort. With a heap of size k ordered by (count, reverse alphabetical), time is O(n log k). In Python, sorting by (-count, word) is simplest at O(u log u) for u unique words. The usual mistake is breaking ties in the wrong direction: the problem wants the lexicographically smaller word first.

    python
    from collections import Counter
    import heapq
    def top_k(words, k):
        c = Counter(words)
        return heapq.nsmallest(k, c, key=lambda w: (-c[w], w))
    java
    import java.util.*;
    class Solution {
        static List<String> topK(String[] words, int k) {
            Map<String, Integer> c = new HashMap<>();
            for (String w : words) c.merge(w, 1, Integer::sum);
            List<String> keys = new ArrayList<>(c.keySet());
            keys.sort((a, b) -> c.get(a).equals(c.get(b)) ? a.compareTo(b) : c.get(b) - c.get(a));
            return keys.subList(0, Math.min(k, keys.size()));
        }
    }
  4. 4

    Best time to buy and sell a stock once: maximise profit given daily prices.

    easy

    One pass tracking the lowest price seen so far; at each day the best profit is price minus that minimum. Time O(n), space O(1). Return 0 if prices only fall. Mistake: taking the global maximum minus the global minimum, which can sell before buying.

    python
    def max_profit(p):
        lo, best = float('inf'), 0
        for x in p:
            lo = min(lo, x)
            best = max(best, x - lo)
        return best
    java
    class Solution {
        static int maxProfit(int[] p) {
            int lo = Integer.MAX_VALUE, best = 0;
            for (int x : p) { lo = Math.min(lo, x); best = Math.max(best, x - lo); }
            return best;
        }
    }
  5. 5

    Product of array except self, without division.

    medium

    Build the answer in two passes: first fill out[i] with the product of everything to the left of i, then sweep from the right keeping a running product of everything to the right and multiply it in. Time O(n), extra space O(1) beyond the output. Dividing by the total product fails when the array contains zeros, which is exactly what the problem is designed to test.

    python
    def product_except_self(a):
        n = len(a)
        out = [1] * n
        for i in range(1, n):
            out[i] = out[i - 1] * a[i - 1]
        right = 1
        for i in range(n - 1, -1, -1):
            out[i] *= right
            right *= a[i]
        return out
    java
    class Solution {
        static int[] productExceptSelf(int[] a) {
            int n = a.length;
            int[] out = new int[n];
            out[0] = 1;
            for (int i = 1; i < n; i++) out[i] = out[i - 1] * a[i - 1];
            int right = 1;
            for (int i = n - 1; i >= 0; i--) { out[i] *= right; right *= a[i]; }
            return out;
        }
    }
  6. 6

    Search for a target in a rotated sorted array of distinct values.

    medium

    Binary search, but at each step one half is sorted. If a[lo] <= a[mid] the left half is sorted: go left when the target lies within [a[lo], a[mid]), otherwise go right; mirror this for the right half. Time O(log n), space O(1). Mistakes: using strict comparisons that skip the case lo == mid, and checking the wrong end of the sorted half.

    python
    def search(a, t):
        lo, hi = 0, len(a) - 1
        while lo <= hi:
            mid = (lo + hi) // 2
            if a[mid] == t:
                return mid
            if a[lo] <= a[mid]:
                if a[lo] <= t < a[mid]: hi = mid - 1
                else: lo = mid + 1
            else:
                if a[mid] < t <= a[hi]: lo = mid + 1
                else: hi = mid - 1
        return -1
    java
    class Solution {
        static int search(int[] a, int t) {
            int lo = 0, hi = a.length - 1;
            while (lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                if (a[mid] == t) return mid;
                if (a[lo] <= a[mid]) {
                    if (a[lo] <= t && t < a[mid]) hi = mid - 1; else lo = mid + 1;
                } else {
                    if (a[mid] < t && t <= a[hi]) lo = mid + 1; else hi = mid - 1;
                }
            }
            return -1;
        }
    }
  7. 7

    Word break: can string s be split into words from a dictionary?

    medium

    Let dp[i] be true if the first i characters can be segmented. dp[0] is true, and dp[i] is true if some j < i has dp[j] true and s[j:i] in the dictionary. Time O(n^2) substring checks (times word hashing), space O(n). Store the dictionary in a set for O(1) lookups; scanning a list inside the loop makes it far slower. Mistake: greedy longest-prefix matching, which fails on 'aaaaab' style inputs.

    python
    def word_break(s, words):
        ws = set(words)
        dp = [False] * (len(s) + 1)
        dp[0] = True
        for i in range(1, len(s) + 1):
            for j in range(i):
                if dp[j] and s[j:i] in ws:
                    dp[i] = True
                    break
        return dp[-1]
    java
    import java.util.*;
    class Solution {
        static boolean wordBreak(String s, List<String> words) {
            Set<String> ws = new HashSet<>(words);
            boolean[] dp = new boolean[s.length() + 1];
            dp[0] = true;
            for (int i = 1; i <= s.length(); i++)
                for (int j = 0; j < i; j++)
                    if (dp[j] && ws.contains(s.substring(j, i))) { dp[i] = true; break; }
            return dp[s.length()];
        }
    }
  8. 8

    SQL: return the top three earners in each department.

    medium

    Rank salaries within each department with DENSE_RANK partitioned by department and keep ranks up to 3. DENSE_RANK keeps everyone tied on a salary, which is normally intended; ROW_NUMBER would cut ties arbitrarily. The window function runs after the join, so join to the department table first or in the outer query. See SQL window functions interview questions.

    sql
    SELECT d.name AS department, e.name AS employee, e.salary
    FROM (
      SELECT name, salary, dept_id,
             DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
      FROM Employee
    ) e
    JOIN Department d ON d.id = e.dept_id
    WHERE e.rnk <= 3;
  9. 9

    Numerical MCQ: a deposit of 10,000 earns 10% a year compounded annually. What is the amount after 2 years?

    easy

    Multiply by 1.1 twice: 10,000 x 1.1 x 1.1 = 12,100. Simple interest would give 12,000, which is the usual distractor. For other periods use A = P(1 + r/100)^n; for half-yearly compounding halve the rate and double the periods. Banking assessments often include this type of question under data interpretation or quantitative aptitude.

  10. 10

    CS MCQ: the maximum number of comparisons binary search needs on a sorted array of 1,024 elements is?

    easy

    Each comparison halves the range, so the worst case is floor(log2 n) + 1 comparisons for a successful or unsuccessful search. For n = 1,024 = 2^10 that is 11. Answer options of 10 often assume log2 n exactly. If the question asks for the number of halvings, count carefully, since the final comparison happens on a single remaining element.

Frequently asked questions

What platform does JPMorgan use for its coding test?

Reports describe HackerRank-based assessments, but platforms can change. Check the instructions in your invitation.

Is SQL part of the JPMorgan assessment?

Some technology tracks are reported to include SQL or database questions. Prepare joins, group by and window functions.

How many problems should I expect?

The number is not published and varies by programme. Prepare for a small set of problems under a fixed time limit.

Are these the actual test questions?

No. They are original practice problems in the style of the topics the round commonly covers.

Related guides

More in Company Interview Questions

See all in this section