Skip to main content

Must-know Algorithms interview questions

16 reported frequent at Airbnb, Databricks, Google and 6 more. “Reported” because the company tags have no public source. Every quick answer is free.

Questions on this page
16
Must know
0
picked by the editor
Reported frequent
16
company tags, reported
Companies
9
with a frequent tag

Reported frequent· 16

Tagged as frequent at a company. Reported: the tags have no public source.

  1. Given a sorted dictionary of an alien language (list of words in lexicographic order), derive the order of its characters (Alien Dictionary).Build edges from the first differing char of each adjacent word pair, then topologically sort the alphabet. Return "" on a cycle or an invalid prefix (longer word before its prefix). O(total chars + V + E).Advanced₹499 (Pro plan required)Reported frequent at Airbnb+1
  2. Given a sorted array, find the k closest integers to a target x (x may not be in the array). Return them in ascending order.The k closest elements are a contiguous window in the sorted array. Binary-search the window's start by comparing arr[mid] and arr[mid+k] distances to x. O(log(n-k)+k). A two-pointer shrink from both ends is an O(n) fallback.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Google
  3. Given an array of words and a maximum line width L, format the text so that each line has exactly L characters and is fully (left and right) justified. Pack as many words per line as possible and distribute extra spaces as evenly as possible; the last line and any line with a single word are left-justified.Greedy-pack words per line while they fit, then spread L - wordLen spaces across k-1 gaps (leftmost gaps get the remainder). Last line and single-word lines are left-justified. O(total chars) time.Advanced₹499 (Pro plan required)Reported frequent at Airbnb
  4. Given exchange rates as a string like "USD:CAD:1.2,CAD:GBP:0.6,USD:AUD:1.4", write a function that converts an amount from a source to a target currency, including multi-step paths (USD to GBP via CAD). Then extend it to return the best achievable rate when several paths exist, and handle rates that form an arbitrage cycle.Parse rates into a directed graph with Decimal weights; BFS multiplies rates to convert across currencies. For the best rate, run Bellman-Ford on -log(rate); a negative cycle means arbitrage and no finite best rate. Dijkstra and max-product DFS can loop forever.Intermediate₹499 (Pro plan required)Reported frequent at Stripe
  5. There are numCourses labeled 0..n-1 with prerequisite pairs. Return an ordering in which you can finish all courses, or empty if impossible (Course Schedule II).Topological sort via Kahn's algorithm: build in-degrees, BFS from zero in-degree nodes, appending to the order. If fewer than n nodes come out, there's a cycle - return empty. O(V+E).Advanced₹99 (Interview Prep or Pro plan required)Reported frequent at Amazon
  6. Count the number of connected components in an undirected graph (or islands in a grid), and discuss the incremental/streaming version.Count components via DFS/BFS (O(V+E)) or Union-Find with path compression + union by rank (near-O(1) per op). For the streaming variant where edges arrive incrementally, Union-Find wins decisively since it updates in near-constant time versus re-traversing per edge. Union-Find handles unions but not deletions.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Databricks
  7. Given a weighted graph, find the minimum-cost path from a source to a destination.Dijkstra with a min-heap for non-negative weights: pop the closest node (its distance is settled), relax neighbors, skip stale heap entries. O((V+E) log V). Follow-ups: Bellman-Ford for negative edges (and cycle detection), Floyd-Warshall for all-pairs, plain BFS if unweighted, A*/bidirectional for scale.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Databricks
  8. House Robber: given an array of house values, maximize the sum you can rob without robbing two adjacent houses.DP recurrence dp[i] = max(dp[i-1], dp[i-2] + value[i]), reducible to two rolling variables for O(n) time, O(1) space. Escalations: circular houses (run linear twice, excluding first or last) and tree-shaped houses (post-order DFS returning rob/skip pair).IntermediateFreeReported frequent at Databricks
  9. Given a string and a dictionary of words, determine whether the string can be segmented into a space-separated sequence of dictionary words (Word Break).DP over prefixes: dp[i] true if some split point j has dp[j] true and s[j:i] in the word set. O(n^2*L), O(n) space. Memoization is what tames the exponential recursion; follow-up is reconstructing the actual segmentation.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Apple
  10. Given a large log/event stream, find the top K most frequent elements efficiently.Count with a hash map, then select top K via a size-K min-heap (O(n + m log K)) or bucket-sort by frequency (O(n)). For distributed/streaming scale, use two-phase aggregation (partial counts then merge, like a distributed GROUP BY) or approximate with a Count-Min Sketch / Space-Saving for bounded memory.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Snowflake
  11. Maximum Points You Can Obtain from Cards: given an array cardPoints and integer k, you take exactly k cards from either the beginning or the end each turn. Return the maximum total points obtainable.Taking k cards from the two ends = leaving a contiguous middle window of size n-k. Minimize that window's sum with a sliding window and return total - minWindowSum. O(n) time, O(1) space. Handle k==n as taking everything.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Uber
  12. Write a method to parse an input string in CSV format. Handle quoted fields that may contain commas and escaped double quotes (a doubled "" inside a quoted field represents a literal quote).Use a character-by-character state machine tracking an in-quotes flag: commas split fields only outside quotes, and a doubled "" inside quotes emits one literal quote. O(n) single pass; test trailing/empty fields and embedded commas.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Airbnb
  13. Given two strings s and t, determine if they are isomorphic - characters in s can be replaced to get t with a consistent one-to-one mapping (no two characters map to the same character). (LeetCode 205 - Isomorphic Strings)Enforce a bijection with two maps (s->t and t->s); a single map wrongly allows two chars to share a target. Or store last-seen index+1 in both directions and reject any disagreement. O(n) time, O(1) space.BeginnerFreeReported frequent at LinkedIn
  14. Given a flowerbed as an array of 0s and 1s (0 = empty, 1 = planted) where no two flowers may be adjacent, determine if n new flowers can be planted without violating the no-adjacent rule. (LeetCode 605 - Can Place Flowers)Greedy left-to-right: plant at each empty plot whose neighbors are both empty (boundaries count as empty), decrement n. Return n<=0. O(n) time, O(1) space. Edge handling at index 0 and last is the key.BeginnerFreeReported frequent at LinkedIn
  15. Implement a retry helper that calls a flaky function (e.g. a payment or API call) and retries on failure using exponential backoff with jitter, a maximum number of attempts, and a maximum delay cap. Only retry on transient/retryable errors.Loop up to max attempts; on a retryable error sleep min(cap, base*2^n) plus random jitter, else re-raise immediately. Use full jitter to avoid synchronized retry storms, and pair retries of state-changing calls with an idempotency key so you never double-charge.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Stripe
  16. Given a menu as a list of item prices and a target value K, find all possible combinations of items that sum to exactly K. (A variation of the classic combination-sum / N-sum problem asked in Airbnb's first round.)Sort prices, then backtrack: at each step include or skip an item, pruning when the price exceeds the remaining target. Pass the same index for reuse-allowed, or index+1 for use-once, and skip equal values at the same depth to dedupe. Exponential worst case with heavy pruning.Intermediate₹99 (Interview Prep or Pro plan required)Reported frequent at Airbnb

Reported frequent at

Must-know questions for other tools