Arian Soleimanzadeh
  • Home
  • Blog
  • Podcasts
  • Videos
  • Library
  • Contact
العربيةArabic
DeutschGerman
EnglishEnglish
فارسیPersian
한국어Korean
中文Chinese
Panel•Quick Contact

Languages

Choose your interface locale

ar

العربية

Arabic

de

Deutsch

German

en

English

English

fa

فارسی

Persian

ko

한국어

Korean

zh

中文

Chinese

Get Appointment

Drop a quick message — I’ll reply as soon as possible.

LinkedInFast response
← Algorithms library/Data Structures/Stack
ALGORITHM READING NOTE

Stack

A stack is a LIFO structure: the last value pushed is the first value popped.

TYPEData structureCATEGORYData StructuresDEPTHBeginnerTYPICAL COMPLEXITYpush/pop O(1)
SHORT INTERVIEW ANSWER

Start with this mental model

A stack is a LIFO structure: the last value pushed is the first value popped.

01
SIMPLE EXPLANATION

How it works, without unnecessary jargon

01

Think of a stack of plates. You add and remove from one end only.

02

Core operations are push, pop and peek/top. With a good implementation they are O(1).

02
EXECUTION

Read the algorithm as a sequence of decisions

01

Push adds to the top.

02

Pop removes and returns the top.

03

Peek reads the top without removing it.

A small example to keep in mind

push(1), push(2), pop() returns 2.

READABLE JAVASCRIPT IMPLEMENTATIONJavaScript
class Stack {
  #items = [];
  push(value) { this.#items.push(value); }
  pop() { return this.#items.pop(); }
  peek() { return this.#items.at(-1); }
  get size() { return this.#items.length; }
}
How to read this code

Do not memorize the syntax. Follow the state that changes on each line and connect it to the invariant described above.

COMPLEXITY

push/pop O(1)

Do not quote this complexity mechanically. Ask which operation or input dimension it describes, then derive the cost from the loops, recursion depth, data-structure operations and any preprocessing assumptions.

03
WHEN TO USE IT

Good fit

  • Undo/redo
  • Expression parsing
  • DFS and backtracking
04
COMMON MISTAKES

What usually goes wrong

  • Popping from an empty stack without handling it.
  • Using Array.shift() and accidentally making operations O(n).
HOW TO EXPLAIN IT IN AN INTERVIEW

A professional answer shape

Say LIFO first, then name push/pop/peek and one real use such as call stacks or parsing.

Problem→Core idea→Invariant / state→Big-O→Trade-off
QUICK INTERVIEW CHECK

What problem does Stack solve?

A stack is a LIFO structure: the last value pushed is the first value popped.

What should I mention about complexity?

Its typical cost here is push/pop O(1), but a complete answer should connect that cost to the representation, input constraints and the dominant operations.

What is the most common mistake?

Popping from an empty stack without handling it.

Continue readingQueueValid ParenthesesDFS
SOURCE & FURTHER READING

This lesson is independently written for calm study and interview preparation, based on the topic catalog from trekhleb/javascript-algorithms. The upstream repository contains the original implementations, tests and per-topic README references.

Open upstream repository ↗
Arian Soleimanzadeh

Personal portfolio focused on modern web engineering, UI systems, and practical AI products — clean code, clean design.

Quick Links

  • About
  • Blog
  • Library
  • Contact

Contact

  • soleimanzadeh.a.work@gmail.com
  • soleimanzadeh.uni@gmail.com

Availability: Weekdays

Typically replies within 24h.

Newsletter

Get updates on posts, projects, and new releases.

© 2026 ariansoleimanzadeh.dev — All rights reserved.

LinkedIn