DSA
    intermediate

    Depth-First Search (DFS)

    Depth-First Search (DFS)

    ⏱ O(V + E)
    💾 O(V)
    Ready
    Speed:50%
    0
    1
    2
    3
    4
    5
    6

    Recursion Call Stack (LIFO)

    Empty Stack
    Stack grows up, top element is current function call

    Initialize DFS: start from node 0

    Step: 1/24Visited: 0 nodesStack depth: 0
    Current
    Visited
    Exploring
    Unvisited

    Go deep first

    Sequence

    Graph: A → B, C · B → D, E · C → F. Visiting neighbours alphabetically, order the nodes as DFS from A visits them.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    6. 6Drop a step here
    6 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What data structure is implicitly used in recursive DFS?

    Write the code

    Runs your solution

    Number of Islands

    Return the number of islands in a 2D binary grid ("1" = land, "0" = water).

    Example Cases

    Input:[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
    Output:1
    Input:[["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
    Output:3
    javascript

    When to reach for this

    • Find **Connected Components**
    • Detect Cycles in a Graph
    • Maze solving / Path existence
    • Topological Sorting (DFS based)

    Interview Questions

    Amazon
    Meta
    Bloomberg
    Meta
    LinkedIn
    Microsoft
    Uber
    Google
    Airbnb
    Amazon
    LinkedIn
    Bloomberg
    Microsoft
    Netflix
    Google

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required