📖 3 min read
Last updated on

Ransom Note — Coding Interview Walkthrough


Ransom Note is a classic character-frequency problem. The idea is simple, but it’s a reliable signal in interviews — it tests whether you reach for a hash map when you should, whether you short-circuit early, and whether you understand the constraint that real magazines don’t re-use letters.


The Problem

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine, and false otherwise. Each letter in magazine can only be used once.

Example

Input: ransomNote = "aa", magazine = "aab" Output: true

Already comfortable with the solution? Practice it in a mock interview →


What Interviewers Are Actually Testing

SkillWhat Interviewers Observe
Frequency countingDo you reach for a hash map or Counter without overthinking it?
Early terminationDo you bail as soon as you detect a missing letter?
Space optimizationCan you do it in one pass over magazine, one pass over ransomNote?
Edge casesEmpty strings, single character, magazine shorter than note?

Step 1: Clarifying Questions

  1. Are characters lowercase only? Yes — you can use a 26-element array instead of a hash map.
  2. Can magazine be shorter than ransomNote? Yes. If so, return false immediately.

Step 2: The Key Insight

Count the supply, then check the demand. Build a frequency map of letters in magazine. For each letter needed by ransomNote, decrement the count. If you ever need a letter that’s been exhausted, the note cannot be constructed.


Step 3: Optimal Strategy

  1. If len(magazine) < len(ransomNote), return false immediately.
  2. Build a Counter (or 26-element array) from magazine.
  3. For each character in ransomNote: if that character’s count is 0, return false. Otherwise, decrement.
  4. Return true.

Python Implementation

from collections import Counter

def canConstruct(ransomNote: str, magazine: str) -> bool:
    magazine_counts = Counter(magazine)

    for char in ransomNote:
        if magazine_counts[char] == 0:
            return False
        magazine_counts[char] -= 1

    return True

26-element array variant (avoids hash overhead):

def canConstruct(ransomNote: str, magazine: str) -> bool:
    counts = [0] * 26

    for char in magazine:
        counts[ord(char) - ord('a')] += 1

    for char in ransomNote:
        idx = ord(char) - ord('a')
        counts[idx] -= 1
        if counts[idx] < 0:
            return False

    return True

Time & Space Complexity

AspectComplexity
TimeO(n + m) — one pass over each string
SpaceO(1) — at most 26 entries

Common Interview Mistakes

  1. Skipping the early-length check. If magazine is shorter than ransomNote, return false immediately — don’t build the map.

  2. Mutating the magazine string. Removing matched characters from a string is O(n) per deletion — avoid.

  3. Not explaining the 26-element array optimization. For lowercase-only inputs, a fixed-size array is O(1) space and avoids hashing overhead.


What a Strong Candidate Sounds Like

“The straightforward approach is a frequency counter on magazine. I iterate through ransomNote and decrement for each character needed. If the count would go negative, the note can’t be built. Time is O(n + m), space is O(1) since we’re limited to 26 lowercase letters.”


Example Interview Dialogue

Interviewer: Can you reduce the space further?

Candidate: Since the alphabet is fixed at 26 lowercase letters, I can use a 26-element array instead of a hash map. Each index corresponds to ord(char) - ord('a'). It’s still O(1) space, but avoids hash collisions and is cache-friendlier in practice.

Interviewer: What’s the minimum length check about?

Candidate: If magazine has fewer characters than ransomNote, there’s no way to satisfy the note regardless of which characters are in it. It’s a cheap O(1) early exit before we build anything.



Practice This in a Mock Interview

Ransom Note is a warm-up problem, but interviewers use it to see if you write clean, efficient code immediately or fumble toward it. The 26-element array optimization and the early-length check are the two things that separate a solid answer from a mediocre one.

Start a mock interview for Ransom Note on Intervu.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →