DEV Community

Saras Growth Space
Saras Growth Space

Posted on

LLD Data Structures in Design Context: Recognizing Stack Problems Before Writing Code

"The best engineers don't memorize that browsers and editors use Stacks. They recognize the behavior that makes a Stack the right solution."

So far in this Stack mini-series, we've learned:

  • why some software problems revolve around recent actions,
  • what a Stack is,
  • and why it naturally supports Undo, Rollback, and Back navigation.

But knowing how a Stack works is only half the skill.

The more valuable skill is recognizing when a problem naturally calls for a Stack.

Let's build that intuition.


Don't Start With the Data Structure

Imagine you're asked to design a collaborative whiteboard application.

A beginner might immediately think:

Should I use

Stack?

Queue?

HashMap?
Enter fullscreen mode Exit fullscreen mode

Experienced engineers begin somewhere else.

They first understand how the business behaves.

Only then do they decide whether a Stack belongs in the design.


Question 1: Does the Business Care About the Most Recent Action?

This is the first question you should ask.

Imagine these requirements.

  • Undo the last drawing.
  • Return to the previous screen.
  • Cancel the latest operation.
  • Restore the previous configuration.

Notice the common pattern.

Latest Action

↓

Needs To Be Revisited
Enter fullscreen mode Exit fullscreen mode

If the newest action always matters first, you're seeing the strongest clue that a Stack may fit naturally.


Question 2: Does the User Move Backward Step by Step?

Some workflows move forward one step at a time but occasionally need to move backward.

For example:

Profile

↓

Address

↓

Payment

↓

Confirmation
Enter fullscreen mode Exit fullscreen mode

Now imagine the user clicks Back.

Where should they return?

Not to the beginning.

They return to the previous step.

That behavior mirrors a Stack.


Question 3: Are Actions Reversed in the Opposite Order?

Think about these operations.

Open File

↓

Edit File

↓

Rename File

↓

Save File
Enter fullscreen mode Exit fullscreen mode

If something goes wrong, which operation should be reversed first?

The latest one.

Whenever work must be undone in the reverse order of execution, a Stack becomes a strong candidate.


A Simple Decision Framework

Whenever you're unsure, follow this sequence.

Does the feature need recent history?

↓

Yes

↓

Will users reverse actions?

↓

Yes

↓

Does the latest action matter first?

↓

Yes

↓

Consider a Stack
Enter fullscreen mode Exit fullscreen mode

Notice that the framework never starts with:

"Which data structure should I use?"

It always starts with understanding the business requirement.


Example: Drawing Application

A designer performs these actions.

Draw Circle

↓

Draw Rectangle

↓

Change Color

↓

Erase Object
Enter fullscreen mode Exit fullscreen mode

Now they press Undo.

The expected behavior is:

Erase Object

↓

Change Color

↓

Draw Rectangle
Enter fullscreen mode Exit fullscreen mode

The application naturally reverses actions from newest to oldest.

That's Stack behavior.


Recognize Behaviors, Not Products

Many beginners memorize examples.

  • Browsers use Stacks.
  • Editors use Stacks.
  • IDEs use Stacks.

Experienced engineers don't memorize applications.

They recognize recurring behaviors.

Whenever they hear requirements like:

  • Undo
  • Redo
  • Back
  • Restore previous state
  • Reverse recent operation

they immediately begin thinking about Stack-like behavior.


Clues That Often Suggest a Stack

Certain requirements should make you pause and think.

Undo

Redo

Back

Rollback

Previous Step

Recent Action

Restore State

Nested Operations
Enter fullscreen mode Exit fullscreen mode

These aren't guarantees.

They're simply clues that the business might naturally revolve around recent actions.


How This Changes Your LLD Design

Before creating classes, identify who should own the action history.

Instead of letting every component manage its own history, introduce a dedicated history manager.

User Action

↓

History Manager

↓

Stack

↓

Undo Request

↓

Restore Previous State
Enter fullscreen mode Exit fullscreen mode

Separating history from business logic makes the design easier to understand, test, and extend.


Common Beginner Mistakes

Mistake 1 — Choosing a Stack Because There Is a Sequence

Many systems process items in sequence.

A Stack is appropriate only when the most recent item must be processed first.


Mistake 2 — Confusing History With Storage

A Stack usually manages recent operational history, not permanent business records.

Don't replace long-term storage with a Stack.


Mistake 3 — Memorizing Examples Instead of Behaviors

Don't remember that browsers use Stacks.

Remember why they use them.

The same reasoning applies to many other systems.


Mistake 4 — Mixing History With Business Logic

Undo management, navigation history, and rollback tracking are responsibilities of their own.

Keeping them separate usually produces cleaner designs.


Engineering Perspective

During design discussions, experienced engineers rarely say:

"This looks like a Stack problem."

Instead, they ask:

  • What happens when the user presses Undo?
  • Can the previous state be restored?
  • Which action should be reversed first?
  • Does this workflow move backward one step at a time?

Those questions naturally reveal whether Stack behavior exists.

The data structure follows from the answers.


The Most Important Insight

The strongest signal for using a Stack isn't the application you're building.

It's the business requirement that the latest action must always be the first one revisited or reversed.

Once you recognize that behavior, Stack-based designs become much easier to identify.


One-Line Takeaway

Don't choose a Stack because you've seen it before—choose it because the business revolves around the most recent action.

Top comments (0)