1. Home
  2. Explore
  3. Computer Science
  4. Graph Algorithms Interview Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Graph Algorithms Interview Course

  • 27Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Graph Basics
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a graph?

Tap to flipClick to flipspace

A collection of nodes (vertices) and edges (connections between nodes).

Or swipe the card

27 cards

  1. What is a graph?

    A collection of nodes (vertices) and edges (connections between nodes).

  2. What is the difference between a node and an edge in a graph?

    A node (vertex) is a data point, and an edge is a connection between two nodes.

  3. What is the difference between a directed graph and an undirected graph?

    Directed graphs have arrows on edges indicating direction; undirected graphs have no direction (two-way streets).

  4. What is a neighbor node in a graph?

    A node that is directly reachable from another node via an edge, obeying edge direction.

  5. What is an adjacency list?

    A hash map (object/dictionary) where each key is a node and its value is an array of its neighbor nodes.

  6. In a depth-first traversal (DFS), what order are nodes visited?

    Nodes are visited by going as deep as possible along one path before backtracking.

  7. In a breadth-first traversal (BFS), what order are nodes visited?

    Nodes are visited level by level, exploring all neighbors of a node before moving to the next level.

  8. What data structures are used for DFS and BFS?

    DFS uses a stack; BFS uses a queue.

  9. How do you implement DFS iteratively?

    Initialize a stack with the start node. While stack not empty, pop node, process it, then push all its neighbors onto the stack.

  10. How do you implement DFS recursively?

    Define a function that takes a node. Process the node, then recursively call the function on each neighbor.

  11. How do you implement BFS iteratively?

    Initialize a queue with the start node. While queue not empty, dequeue node, process it, then enqueue all its neighbors.

  12. In the 'has path' problem, what is the goal?

    Given a directed acyclic graph (adjacency list) and two nodes (source and destination), return true if a path exists from source to destination, else false.

  13. How do you solve 'has path' using DFS recursively?

    If source equals destination, return true. Otherwise, for each neighbor of source, recursively call hasPath(neighbor, destination). If any call returns true, return true. After loop, return false.

  14. How do you solve 'has path' using BFS iteratively?

    Initialize queue with source. While queue not empty, dequeue node. If node equals destination, return true. Otherwise, enqueue all its neighbors. If loop ends, return false.

  15. What is the time and space complexity of graph traversal (DFS/BFS) in terms of nodes (n) and edges (e)?

    Time: O(e) (each edge visited once). Space: O(n) (stack/queue and visited set could hold all nodes).

  16. If only n is used for graph traversal complexity, what are the time and space?

    Time: O(n^2) (maximum edges is n^2). Space: O(n).

  17. How do you convert an edge list to an adjacency list for an undirected graph?

    Create an empty object. For each edge [a, b], if a not in graph, set graph[a] = []; if b not in graph, set graph[b] = []; then graph[a].push(b) and graph[b].push(a).

  18. Why is a visited set necessary when traversing undirected graphs?

    To avoid infinite loops caused by cycles (e.g., traveling back and forth between two nodes).

  19. How do you count connected components in an undirected graph?

    Initialize count = 0 and visited set. For each node in graph, if node not visited, run DFS/BFS from it, marking all reachable nodes as visited, then increment count. Return count.

  20. How do you find the largest connected component size?

    Similar to counting components, but track the size of each component during traversal. Keep a variable 'largest' and update it when a component size is larger.

  21. Why is BFS preferred over DFS for finding the shortest path (number of edges)?

    BFS explores nodes in order of distance from start; the first time the target is reached, it is guaranteed to be the shortest path.

  22. How do you track distance in BFS for shortest path?

    Store pairs (node, distance) in the queue. Initialize start node with distance 0. When processing a node, for each neighbor, enqueue (neighbor, distance+1).

  23. What is a grid graph?

    A grid where each cell (position) is a node, and edges connect to up/down/left/right neighbors (4-directional).

  24. How do you count islands in a grid (connected 'L' cells)?

    Use nested loops over rows and columns. For each cell that is land and not visited, run DFS/BFS to mark all connected land, increment island count. Use visited set to avoid re-exploration.

  25. How do you find the minimum island size?

    Similar to island count, but during DFS/BFS, compute the size of each island (number of cells). Track the minimum size across all islands.

  26. How should you represent grid positions in a JavaScript Set to avoid reference equality issues?

    Use a string like ${row},${col} (e.g., "3,4") instead of an array.

  27. What are the base cases for a DFS on a grid?

    Check: out of bounds (row or column invalid), current cell is water ('W'), or the cell has already been visited.

Card 1 of 27
Card 1 of 27

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent