← J.P. Morgan Interview Insights

J.P. Morgan·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Went through a coding screen for a software engineer role at J.P. Morgan. One algorithmic question, pretty straightforward on the surface but the constraints matter more than you'd think.

Questions Asked (1)

Q1

Given a string, find the index of the first character that appears exactly once in the entire string. Return -1 if no such character exists.

Algorithms & Data Structures
Author's notes

Classic frequency map problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pass approach: first count the frequency of each character, then iterate through the string to find the first character with a count of exactly one. This ensures O(n) time and O(1) space for fixed character sets like ASCII.

Pro tip: Clarify the character set (e.g., ASCII vs Unicode) upfront, as it affects space complexity and implementation. Mention that for ASCII, a fixed-size array of 256 integers is more efficient than a hash map.

1. Clarify requirements and constraints

Ask about the character set (ASCII, Unicode), string length, and whether the string can be empty. This determines the appropriate data structure and complexity analysis.

2. Choose data structure for frequency counting

For ASCII, use an array of size 256; for Unicode, use a hash map. Explain the trade-offs in terms of space and time.

3. First pass: count frequencies

Iterate through the string once, incrementing the count for each character in the chosen data structure.

4. Second pass: find first unique character

Iterate through the string again, and return the index of the first character whose count is exactly 1. If none found, return -1.

5. Analyze complexity and edge cases

State time complexity O(n) and space complexity O(1) for ASCII (or O(k) for Unicode). Discuss edge cases like empty string, all repeating characters, and single character.

Key Points to Mention

  • Time complexity: O(n) with two passes, which is optimal.
  • Space complexity: O(1) for ASCII using fixed array, O(k) for Unicode using hash map.
  • Trade-offs between array and hash map based on character set.
  • Edge cases: empty string, string with all duplicates, string with one unique character.
  • Alternative approaches: using LinkedHashMap to preserve order and count in one pass, but still O(n) space.
  • Importance of clarifying assumptions before coding.

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