Interview Q&A · questions with model answers

Algorithms Interview Questions

Part of the study plan for: Machine Learning Engineer

Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.

  1. Most asked Explain the sliding window pattern.

    Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n).

  2. Most asked Detect a cycle in a linked list—how?

    Floyd: slow/fast pointers; if they meet, a cycle exists.

  3. Most asked Reverse a singly linked list iteratively.

    Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev.

  4. Most asked How does Dijkstra work at a high level?

    Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights.

  5. Most asked Topological sort—requirements and uses?

    Only for DAGs; orders nodes so every edge u→v has u before v.

  6. Most asked Name five high-frequency coding patterns.

    Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find.

  7. Most asked LRU cache—data structures?

    Hash map key→node plus doubly linked list for O(1) move-to-front and eviction.

  8. BFS uses which structure and why?

    A FIFO queue so nodes are processed level by level / in discovery order.

  9. Min-heap operations complexity?

    Peek min O(1); insert and extract-min O(log n) for binary heap.

  10. Inorder traversal of a BST yields what?

    Keys in sorted ascending order (given standard left < node < right ordering).

  11. Lowest common ancestor in a binary tree (not BST)?

    Recurse left/right; if both sides find a target, node is LCA; else return the non-null side.

  12. Detect a cycle in a directed graph?

    DFS colors (visiting recursion stack) or Kahn's algorithm failing to order all nodes.

  13. Does recursion use extra space?

    Yes—call stack depth.

  14. How do prefix sums help?

    Precompute cumulative sums so any range sum is O(1) via prefix[r]-prefix[l-1].

  15. Kadane's algorithm solves what?

    Maximum subarray sum in O(n) by tracking best ending here / best overall.

  16. Find the middle of a linked list in one pass.

    Slow moves 1, fast moves 2; when fast ends, slow is at middle.

  17. Merge two sorted linked lists—approach?

    Iterate both with a dummy tail, always attach the smaller node; append remainder.

  18. k-th smallest in a BST?

    Inorder traversal counting visits; stop at k.

  19. Binary search bug that causes infinite loops?

    Mishandling mid and bounds so the search interval never shrinks (e.g., always setting lo=mid on integers).

  20. Search in a rotated sorted array—key idea?

    One half is always sorted; decide which half contains the target using mid vs ends.

  21. Classic recurrence for Fibonacci and its DP cost?

    F(n)=F(n-1)+F(n-2); naive exponential, memo/tabulation O(n) time O(n) or O(1) space rolling.

  22. Longest common subsequence—complexity of standard DP?

    O(m·n) time and space for strings of lengths m,n (space can be optimized to O(min(m,n))).

  23. Edit distance measures what?

    Minimum insert/delete/replace ops to transform one string into another; classic DP on prefixes.

  24. House Robber intuition?

    At each house: max(skip + rob previous best, take + rob best two back); linear DP.

  25. Grid unique paths DP state?

    dp[i][j] = paths to cell (i,j) = from above + from left, with first row/col base cases.

  26. Serialize and deserialize a binary tree—common approach?

    Preorder with null markers, or level-order BFS encoding; parse with the same convention.

  27. Is Python's sorted stable?

    Yes—Timsort is a stable mergesort hybrid.

  28. Name a probabilistic alternative to exact set membership.

    Bloom filter—fast, space-efficient, allows false positives but not false negatives (typically).

Was this page helpful?

Report a mistake