← JP Morgan Chase Interview Insights

JP Morgan Chase·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Apr 2026

Summary

JP Morgan Chase coding round, got a math-heavy array problem that I almost overthought into oblivion. Not a bad experience but the problem felt more like a puzzle than anything I'd write at work.

Questions Asked (1)

Q1

Given a list of song durations, find the number of pairs whose total length is divisible by 60.

Algorithms & Data Structures
Author's notes

My first instinct was brute force, two nested loops, check every pair.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., list size, duration range) and discuss the brute-force O(n^2) approach first. Then optimize using a hash map to count remainders modulo 60, handling pairs in O(n) time. Explain how to avoid double-counting and handle edge cases like remainder 0 and 30.

Pro tip: Mention that you would use a frequency array of size 60 instead of a hash map for O(1) lookups and better cache performance, and discuss how to handle large inputs with streaming if needed.

1. Understand the problem

Restate the problem: given a list of song durations, count pairs (i, j) with i < j such that (durations[i] + durations[j]) % 60 == 0. Ask clarifying questions about input size, duration range, and whether durations can be zero.

2. Discuss brute-force approach

Explain the naive O(n^2) solution: iterate over all pairs and check divisibility. Mention its time complexity and that it's inefficient for large n.

3. Optimize with remainder counting

Use a hash map or array to count frequencies of each remainder modulo 60. For each duration, compute its remainder r and add the count of (60 - r) % 60 seen so far to the answer, then increment the count for r.

4. Handle edge cases and avoid double-counting

For remainder 0, pairs are formed within the same group: add count * (count - 1) / 2. For remainder 30, similarly. For other remainders, pair r with 60-r, ensuring each pair is counted once.

5. Analyze complexity and test

State time complexity O(n) and space O(1) (since 60 is constant). Walk through a small example to verify correctness, and discuss potential integer overflow if n is large.

Key Points to Mention

  • Modulo arithmetic: (a + b) % 60 == 0 implies (a % 60 + b % 60) % 60 == 0.
  • Using a frequency array of size 60 for O(1) lookups and O(1) space.
  • Handling remainders 0 and 30 separately to avoid double-counting.
  • Time complexity O(n) and space complexity O(1).
  • Edge cases: empty list, single element, large n, durations up to 10^9.
  • Alternative: sorting and two-pointer approach (O(n log n)) but less efficient than counting.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.