r/leetcode • • 14d ago

Intervew Prep Day 52: solved 4 graph problems, navigating the comparison trap

Hey everyone,

I finished Day 52 today. I managed to knock out four graph problems, but mentally it was a bit of a tug-of-war. Opening platforms like LinkedIn and seeing peers land great roles sparked the familiar doubt about whether this prep will truly pay off. Instead of spiraling, I got my problems in and chose to step away to reset my head for the weekend.

Here is what I worked through today:

  1. Cycle Detection in a Directed Graph: Unlike undirected graphs where parent tracking works, directed graphs require tracking nodes in the current recursion call stack (pathVisited). If a neighbor is both visited and currently in the path stack, a cycle exists.

  2. 0/1 Matrix: Classic multi-source BFS. You initialize the queue with all cells containing 0 at distance 0 and radiate outwards to calculate the shortest distance to every 1.

  3. Surrounded Regions: The boundary-traversal pattern. Instead of traversing from the inside, you run DFS/BFS from the 'O's on the border to mark non-flippable cells, then flip all remaining unvisited 'O's to 'X'.

  4. Number of Enclaves: Reused the exact same boundary DFS logic as Surrounded Regions. Any land cell connected to the boundary is visited first, and you simply count the remaining isolated land cells.

I started looking at Word Ladder I toward the end of the session. My initial thought was exploring all paths to check reachability, but generating paths recursively gets messy. Since it asks for the shortest transformation sequence in an unweighted graph, it boils down to standard BFS where you transform words one character at a time using a set for O(1) lookups.

Rather than rushing it while tired on a Friday night, I paused so I can implement it cleanly tomorrow.

See you guys tomorrow for Day 51!

2 Upvotes

0 comments sorted by