Sitemap

Dynamic Array: Understanding the Problem Before Writing Code

3 min readSep 12, 2026

--

What HackerRank’s “Dynamic Array” Problem Actually Taught Me

When I opened the Dynamic Array problem on HackerRank, I expected the usual — read the constraints, write some Java, submit. But this one didn’t fight me on the coding side. It fought me on the story.

I sat there re-reading the problem statement for longer than I’d like to admit, not because the syntax was hard, but because I genuinely didn’t understand what was being asked.

Where I Actually Got Stuck

Here’s the honest list of things that confused me, in the order I hit them:

  • What are these two query types even supposed to represent?
  • In 1 x y and 2 x y — what is x? What is y? The problem throws these letters around like I should already know.
  • Why does the formula keep dragging in this lastAnswer thing and XOR-ing it with x?
  • And once I saw (x ^ lastAnswer), where on earth did the % n come from?

None of this was a Java problem. It was a translation problem — I hadn’t converted the words into operations yet.

The part that really unlocked it for me was realizing y doesn't mean the same thing in both queries:

  • In a type 1 query, y is the value you're adding.
  • In a type 2 query, y is used to pick which element you want out of the array you land on.

Same letter, two completely different jobs depending on context. Once that clicked, the rest of the problem stopped feeling like a wall of symbols.

Get VIGNESH’s stories in your inbox

Join Medium for free to get updates from this writer.

The other piece that took me a minute was this: lastAnswer isn't just some leftover variable sitting in the corner — it changes after every type 2 query, and because it changes, the same x can point to a completely different array the next time you use it. That's the whole trap of the problem. You can't just compute an index once and reuse your intuition; you have to actually track state.

The Mental Model That Finally Made Sense

After going back and forth on this, here’s the model I landed on:

1 x y
→ find an array
→ add y
2 x y
→ find an array
→ get an element using y
→ update lastAnswer

And the array itself is always chosen the same way, regardless of query type:

idx = (x ^ lastAnswer) % n

Once I had it framed this way — as “find the array, then do one of two things to it” — writing the actual Java became almost boring in comparison:

public static List<Integer> dynamicArray(int n, List<List<Integer>> queries) {
ArrayList<ArrayList<Integer>> arr = new ArrayList<>();
int lastAnswer = 0;
ArrayList<Integer> answers = new ArrayList<>();

for (int i = 0; i < n; i++) {
arr.add(new ArrayList<Integer>());
}

for (List<Integer> query : queries) {
if (query.get(0) == 1) {
int idx = (query.get(1) ^ lastAnswer) % n;
arr.get(idx).add(query.get(2));
} else if (query.get(0) == 2) {
int idx = (query.get(1) ^ lastAnswer) % n;
lastAnswer = arr.get(idx).get(query.get(2) % arr.get(idx).size());
answers.add(lastAnswer);
}
}

return answers;
}

I traced through the sample input by hand before trusting this — n = 2 with queries 1 0 5, 1 1 7, 1 0 3, 2 1 0, 2 1 1 — and watching lastAnswer flip from 0 to 7 mid-way through, which then changed which array the last query pointed to, is what actually convinced me the formula was right. The final answers came out as 7 and 3, matching what HackerRank expected.

The Real Lesson Here

Looking back, I wasn’t actually struggling with arrays or with Java. I was struggling to turn a story-based problem statement into a set of plain operations.

That’s made me change how I approach these DSA problems now. Before writing a single line of code, I try to answer four questions first:

  1. What data do I actually have?
  2. What operations can happen to it?
  3. What does each input value mean?
  4. What am I supposed to output at the end?

Only after I can answer all four do I let myself start coding.

It sounds obvious written out like that. But it took getting genuinely stuck on a “simple” array problem for me to actually internalize it.

--

--

VIGNESH
VIGNESH

Written by VIGNESH

0 followers

Learning by building, solving DSA problems, contributing to open source, and sharing what I learn along the way.