Kartik Madhira

Leetcode patterns for ML interviews

I am going to list here some of the most commonly asked LC-style question patterns.

Graphs

For me, almost 90% of the time I have been asked graph based questions. Lets see some of the patterns in these.

DFS

Most of the graph based and backtracking based problems can be solved with DFS.

Common lookout patterns appear to be:

  1. Coverage based - Given grid -> find covered area. Max Area of Island

  2. Tracing a path - Given grid -> find locations in the trace. Pacific Atlantic Water Flow

  3. Directed graphs - Given connections -> find if path possible. Course Schedule II

  4. Cycle Detection in a directed/undirected graph. Redundant Connection

Other must do questions:

  1. Number of Connected Components in an Undirected Graph
  2. Clone graph
  3. Graph Valid Tree

BFS

This one is a bit tricky and most of the times in the past, I have been able to identify a graph problem but structuring it into bfs or dfs can be tricky.

Common lookout patterns appear to be:

  1. ā€˜n’ goals/distance to ā€˜n’ goals - Given grid and n no. of goals -> get least distance to each cell. Islands and Treasure
  2. Minimum time t type (variant of 1) - Given grid and n no. of goals -> get least time to cover cells. Rotting Fruit