Skip to content

Instantly share code, notes, and snippets.

@frankstepanski
Last active January 2, 2022 22:29
Show Gist options
  • Save frankstepanski/b51b8030536c8d50e2a5ebdf407f873d to your computer and use it in GitHub Desktop.
Save frankstepanski/b51b8030536c8d50e2a5ebdf407f873d to your computer and use it in GitHub Desktop.
Interview Prep Algo - Stacks, Queues & Linked Lists

Stacks, Queues, & Linked Lists

The main purpose of this lecture is to identify moments in problems that will lead us to decide on these data types/data structures as useful approaches in interviewing problems.

When do we use stacks?

  • Direct implementation questions (i.e. build a stack with 2 queues)
  • Depth-first search/traversal (DFS) of trees/matrices/graphs
    • a subset of backtracking
  • Iterative implementation of recursive algorithms (i.e. instead of a callstack, using an actual stack in the function)
  • Indirect implementations
    • evaluation arithmetic expressions (postfix, prefix, infix)
    • vague questions (online stock span, next greater element, balancing parentheses)
    • These require a look at what the question needs, and how the elements rely on the previous ones closest to it.

When do we use a queue?

  • Direct implementation questions
  • Breadth-first search/traversal (BFS) of trees/matrices/graphs
    • Think: You don't want to deal with it now, but you WILL need to deal with it later.
    • Think: You are waiting to complete something so you queue it up.

Approach: Stack or Queue? (Leetcode)

Useful/Common Stack/Queue Problems (Leetcode)

When do we use a linked list?

  • Direct implementation
    • implementation for stacks/queues storage
    • build linked lists from scratch, or build new nodes (using constructors and classes)
    • change rules for linked lists
    • Manipulate an existing linked list (provided the head/root node, or any node in the list, or the list itself)

Linked List Manipulation (GeeksForGeeks)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment