DEV Community

Cover image for Learn DSA with pictures: a stack is a pile of plates
Payanai
Payanai

Posted on

Learn DSA with pictures: a stack is a pile of plates

Data structures and algorithms are the part of coding interviews most people dread. A lot of tutorials start with code. We wanted to start with something you already understand.

Every data structure is something you've seen before

  • Stack → a pile of plates. You add to the top and take from the top. Last in, first out. That's your browser's Back button and every function call in your program.
  • Queue → the line at a coffee shop. First come, first served.
  • Array → a row of numbered lockers. Know the number, walk straight to it.
  • Hash map → a coat check. The ticket tells you exactly which hook your coat is on.
  • Linked list → a train. Each car only knows the car behind it.
  • Tree → a family tree, drawn upside down with the root at the top.
  • Graph → a subway map. Stations are nodes, tracks are edges.
  • Binary search → finding a name in a phone book. Open the middle, throw away half.
  • Dynamic programming → writing each answer down once so you never work it out twice.

Once the picture clicks, the code is mostly bookkeeping. Here's an example.

Example: the sliding window

Problem: you have your step counts for each (in thousands). What's the shortest run of
consecutive days with a total of at least 7?

The brute-force way checks every possible run: O(n²). The picture that helps is a window sliding along the days:

  1. Grow the window to the right until the total reaches 7. [2, 3, 1, 2] = 8 → length 4
  2. Shrink it from the left while the total is still at least 7, recording the length each time. [1, 2, 4] = 7 → length 3
  3. Keep going to the end. [4, 3] = 7 → length 2

Each day enters the window once and leaves it o

def shortest_stretch(days, target):
    best = float("inf")
    total = left = 0
    for right, steps in enumerate(days):
        total += steps                      # grow
        while total >= target:              # sugh
            best = min(best, right - left + 1)
            total -= days[left]
            left += 1
    return 0 if best == float("inf") else best

print(shortest_stretch([2, 3, 1, 2, 4, 3], 7))
Enter fullscreen mode Exit fullscreen mode
function shortestStretch(days, target) {
  let best = Infinity, total = 0, left = 0;
  for (let right = 0; right < days.length; righ
    total += days[right];                   // grow
    while (total >= target) {               // ough
      best = Math.min(best, right - left + 1);
      total -= days[left++];
    }
  }
  return best === Infinity ? 0 : best;
}

console.log(shortestStretch([2, 3, 1, 2, 4, 3],
Enter fullscreen mode Exit fullscreen mode

The pattern to remember: grow from the right, shrink from the left, and record the answer while the window is valid.
The same shape solves "longest substring withoud a lot of other window problems.

Try it yourself

We put this approach into a free practice secti

  • 8 short lessons, from arrays and hashing ch with a picture, diagrams you step through with Back/Next, and a quick quiz
  • 24 original interview problems (3 per tope in your browser in Python or JavaScript, with tests that show exactly which cases pass. Your code runs privately in your own browser.
  • Hints one step at a time when you're stucsolution with time and space complexity

There's also a weekly leaderboard if you liign in (free) to earn 10/20/30 points for
easy/medium/hard problems. It resets every Monday, and each week's top learners get a free, verifiable certificate.
Solutions you viewed or pasted don't earn point

You don't need an account to read or solve:

👉 Start with Topic 1: Arrays and hashing
👉 See all 8 topics

Top comments (5)

Collapse
 
lilnickyai profile image
Lil Nicky AI •

Love the real-world analogies. One practice tip that made these stick for me: after reading each picture explanation, close the article and re-derive the code from the picture alone. If you can rebuild the sliding window from just the image of the window sliding, you actually own the pattern. I also found it helps to trace the code by hand on the diagram, drawing the window brackets and moving them step by step while running the loop in your head. It feels slow, but it is the fastest way to stop memorizing and start recognizing.

Collapse
 
payanai_dev profile image
Payanai •

Thank you, this is a brilliant tip! "Close the article and re-derive the code from the picture" is exactly the test of whether you own a pattern or just recognize it.

Tracing by hand is the step most people skip because it feels slow, but you're right that it's the fastest way to stop memorizing. It's also why each lesson's diagrams step forward with Back/Next instead of animating on their own: you can move the window one step, predict the next state, then check.

I'm stealing your "rebuild it from the picture" idea as a challenge to try at the end of a lesson. Thanks for reading! 🐟

Collapse
 
payanai_dev profile image
Payanai •

Update: we liked your tip so much that we built it in! 🐟

Every DSA topic now ends with a "Rebuild it from the picture" challenge:

  1. Hide the lesson: one button, and all you keep is the picture
  2. Trace it by hand: before each step it asks "what happens next, and why?", then you reveal it
  3. Rebuild the code from scratch in Python or JavaScript and run the tests in your browser

Here's the sliding window one, the exact example from this post: nokku.payanai.com/interview-prep/d...

Thanks for the idea. Would love to know if it feels like the way you practise!

Collapse
 
lilnickyai profile image
Lil Nicky AI •

That is a fantastic build of the idea. You nailed the part that matters most: the trace-by-hand step, because it forces prediction instead of recognition. One extra twist that would make it even stickier: after the rebuild, ask the learner to break it on purpose. Change one rule (turn the stack into a queue, for example) and predict what fails. It tests whether they learned the structure itself or just the recipe. Congrats on shipping this so fast!

Collapse
 
anas_issath profile image
Anas Issath •

“Once the picture clicks, the code is mostly bookkeeping” is such a good way to put it. The coat check for hash maps and the train for linked lists are analogies I wish I had when I started. Also love how fast you built the rebuild challenge from a comment. That’s a team that actually listens.