Skip to main content

Arrays and Hash Maps: The Patterns Behind Most Questions

Many coding interviews turn into search, counting, grouping, or fast lookup by key. Arrays and hash maps sit at the center of that most of the time.

Andrews Ribeiro

Andrews Ribeiro

Founder & Engineer

The problem

A lot of people study interviews as if each question came from a different planet.

In practice, a huge part of them is a variation of the same core:

  • traverse a sequence
  • remember something that already appeared
  • count frequency
  • find a complement
  • group by key

The name of the question changes.

Its heart often does not.

That is why arrays and hash maps show up so much.

Not because interviewers have a fetish for basic structures.

But because many useful logic and data structure problems fit exactly in that space.

Mental model

An array is the natural structure when you are dealing with sequence, position, order, and traversal.

A hash map enters when the solution needs to answer something quickly by key.

Classic examples:

  • “have I seen this value before?” -> Set
  • “how many times did this character appear?” -> frequency Hash Map
  • “at which index did I see this number?” -> value-to-index Hash Map
  • “which items belong to the same group?” -> grouping Hash Map

If you want the shortest version:

A lot of interview questions are just a sequence plus some well-chosen auxiliary memory.

Breaking the problem down

First check whether the problem is about sequence

If the main input is:

  • array
  • list
  • string

then in practice you are already dealing with a sequence.

That usually means:

  • one or more traversals
  • comparison between positions
  • maintaining a window
  • prefix, suffix, or accumulated count

The plain array already solves a lot.

Then ask what the solution needs to remember

That question separates a slow solution from an efficient one very quickly.

If the answer is “I need to remember what already appeared,” a Set enters the conversation.

If the answer is “I need to know how many times it appeared,” a frequency map enters.

If the answer is “I need to find the previous index,” a value-to-index map enters.

If the answer is “I need to gather things by category,” grouping by key enters.

That is the really important part.

It is not memorizing Two Sum.

It is noticing that the problem wants fast lookup over things already seen along the way.

The three most common formats

1. Presence

Questions like:

  • is there a duplicate?
  • have I seen this value?
  • does this element belong to the set?

Usually ask for Set.

2. Frequency

Questions like:

  • which character appears the most?
  • are two strings anagrams?
  • which number appears k times?

Usually ask for a counting Hash Map.

3. Index or complement

Questions like:

  • is there a pair that sums to target?
  • where did the complement appear?
  • what was the last position of this value?

Usually ask for a map from value to associated information.

Do not skip the baseline

Even when you recognize the pattern early, it is worth stating the simple version first.

For example:

“This could be solved with two loops, but that would repeat work. Since the question needs fast lookup by a previously seen value, I would switch to a Hash Map.”

That shows evolution of reasoning.

Not only conditioned reflex.

Simple example

Take the classic question:

Given an array of integers and a target, find two indices whose sum equals the target.

Baseline

Compare each element with all the others.

That works.

But it costs O(n²).

Version with hash map

Traverse the array once.

For each number:

  1. calculate the missing complement
  2. check whether it already appeared
  3. if it did, you found the answer
  4. if not, store the current number with its index

Now the reasoning becomes:

  • sequence: array
  • needed memory: previously seen value with index
  • chosen structure: Hash Map

Average time: O(n).

Space: O(n).

The pattern behind the question is not “remember the Two Sum solution.”

It is:

I need to quickly query something that appeared earlier in the traversal.

Common mistakes

  • Using a Hash Map without being able to explain what the key represents.
  • Jumping to a sophisticated structure when a simple Set already solves it.
  • Forgetting that the array still matters for order, position, and traversal.
  • Treating a frequency problem like a presence lookup.
  • Memorizing a ready-made solution and freezing when the question changes one detail.

How a senior thinks

People with more experience almost never start from the famous name of the problem.

They start from the shape of the data and the dominant question.

The reasoning usually goes:

  • I am traversing a sequence
  • what do I need to remember?
  • how long do I need to remember it?
  • is presence enough, or do I need to count, index, or group?

That greatly reduces the amount of “tricks” you think you need to memorize.

In real work, this same skill shows up in parsing, analytics, deduplication, feeds, validation, and data transformation.

So this is not interview-only knowledge disconnected from reality.

It is basic data structure reasoning.

What the interviewer wants to see

They want to notice that you:

  • see the structural pattern of the problem
  • know how to build a baseline
  • improve the solution for a clear reason
  • choose the right structure for the kind of memory you need

If you say “this feels like a sequence problem with fast lookup over what already passed, so a Hash Map makes sense,” your answer already started mature.

Many problems look different on the surface. Underneath, they only ask for well-chosen memory.

When you understand the pattern, you stop hunting for tricks and start making decisions.

Quick summary

What to keep in your head

Practice checklist

Use this when you answer

You finished this article

Next article Dynamic Programming: How to Identify the Problem Before Attacking It Previous article What Interviewers Look for in Practical Exercises Beyond the Code

Keep exploring

Related articles