All categories

Category

DSA & Algorithms

Curated frontend friendly DSA: BFS/DFS, sliding window, binary search, trees, and patterns.

24 questions

24 of 24
DSA & Algorithms
Medium
How do you approach graph based optimization problems in interviews?

Catch-all category. Common forms: shortest path (BFS for unweighted, Dijkstra for weighted, Bellman-Ford for negative edges, A* with heuristic), minimum spanning tree (Kruskal/Prim), max-flow/min-cut (Ford-Fulkerson), strongly connected components (Tarjan/Kosaraju), topological sort (DFS or Kahn). Recognize the problem shape, then pick the algorithm.

5 min
DSA & Algorithms
Medium
How do you optimize a brute force approach during a coding interview?

A general technique question: start with a correct brute force, analyze its complexity to find the bottleneck, then apply a pattern — hashing for lookups, sorting, two pointers, sliding window, precomputation/prefix sums, memoization/DP, or better data structures — to cut redundant work.

5 min
DSA & Algorithms
Medium
How does the sliding window technique solve substring and subarray problems?

Maintain a window [l, r] over an array/string; expand by moving r, shrink by moving l when the window violates a constraint. Turns O(n²) brute force into O(n) for problems like 'longest substring without repeating chars', 'min window substring', and 'max sum of subarray of size k'.

3 min
DSA & Algorithms
Medium
How would you check whether a graph is connected?

Run a single BFS/DFS from any node and check whether every node was visited. O(V+E). For directed graphs, distinguish weak vs strong connectivity (strong needs Kosaraju/Tarjan or a check from one node on both the graph and its transpose).

5 min
DSA & Algorithms
Easy
How would you compare two linked lists for equality?

Walk both lists in lockstep with two pointers; at each step compare values; return false on first mismatch or unequal length. O(min(m,n)) time, O(1) space. Variants: compare for value-equality regardless of node identity; deep-compare lists of lists (recursion); compare cyclic lists (detect cycle with Floyd's, then compare lengths + values).

3 min
DSA & Algorithms
Easy
How would you find the frequency of each element in an array?

Reduce into a Map (or plain object): for each element, increment its count. `new Map` if keys may be objects or non-strings; object literal if all keys are strings/numbers (faster lookup, JSON-friendly). `Object.create(null)` to avoid prototype-chain key collisions like `__proto__`.

3 min
DSA & Algorithms
Easy
How would you find the top K elements in an array?

Three approaches: (a) sort and slice — O(n log n), simplest, fine for small n; (b) min-heap of size k — O(n log k), best when n ≫ k; (c) Quickselect — O(n) average, O(n²) worst, in-place. Pick by data size + whether you need elements in sorted order.

4 min
DSA & Algorithms
Easy
How would you populate next right pointers in each node of a binary tree?

For each node in a binary tree, point its `next` to the node immediately to its right at the same level (or null if last). Two approaches: BFS using a queue (O(n) time, O(n) space); level-by-level traversal using the `next` pointers built in previous levels for O(1) extra space (the canonical perfect-tree solution).

4 min
DSA & Algorithms
Easy
How would you return the kth largest element in an array?

Three approaches: (1) sort and index — O(n log n), simple; (2) min-heap of size k — O(n log k); (3) Quickselect — average O(n), worst O(n²). For most interviews say 'min-heap of size k' (clean, predictable) or Quickselect (best average).

4 min
DSA & Algorithms
Easy
How would you solve the zombie spread problem on a grid using multi source BFS?

Classic multi-source BFS on a grid (a.k.a. 'rotting oranges'). Seed a queue with ALL initial zombie cells at once, then BFS level by level — each level is one time unit. Track minutes as you process levels; mark cells visited as you infect them. At the end, if any human remains, return -1. Time O(rows×cols), space O(rows×cols).

6 min
DSA & Algorithms
Easy
How would you solve Word Break II, segmenting a string using a dictionary?

Given a string `s` and a dictionary, return all ways to segment `s` into dictionary words. DFS/backtracking with memoization on the suffix: for each position, try every dictionary word that matches the prefix and recurse on the remainder; cache results by start index. Time worst-case exponential; memoization makes practical inputs tractable.

4 min
DSA & Algorithms
Easy
What are the common interview applications of stacks and queues?

Stacks: LIFO — undo/redo, browser history, balanced parens, expression eval, DFS, monotonic stack (next-greater-element). Queues: FIFO — BFS, task scheduling, event loops, request batching, sliding-window-max via a deque. In JS both are arrays with push/pop or push/shift (or a deque-like structure).

3 min
DSA & Algorithms
Easy
What are the common variants of binary search you should know for interviews?

Classic: find a target in a sorted array in O(log n). Variants: first/last occurrence, lower/upper bound, search in rotated sorted array, search the answer (binary search on a predicate over a range — capacity, threshold). The pattern is 'monotonic predicate over an index range'.

4 min
DSA & Algorithms
Medium
When should you reach for the two pointer technique?

Use two pointers when scanning a sorted array or linked list and a nested loop would be O(n²). Variants: opposite ends converging (sorted two-sum, palindrome, container with most water), and fast/slow (cycle detection, middle of list). Turns O(n²) into O(n) with O(1) space.

4 min
DSA & Algorithms
Medium
When should you use BFS versus DFS when traversing a graph?

BFS (queue): shortest path in an unweighted graph, level-order, 'closest match'. DFS (stack/recursion): connectivity, cycle detection, topological sort, path enumeration, backtracking. BFS uses more memory (frontier width); DFS uses less but can recurse deep. Both must track visited.

4 min