Coding questions.
79 codingquestions from the bank — open to read. Pick one and practice it out loud; a coach note comes back in seconds.
All 79 questions
Write a function that takes a list of meeting intervals and returns the minimum number of rooms required to schedule all of them.Given a string, return the length of the longest substring without repeating characters. Your function should run in linear time.Implement a least-recently-used cache with constant-time get and put. The capacity is fixed at construction.Write a function that takes a stream of integers and returns the running median after each insert.Given a 2D grid of 1s and 0s representing land and water, return the number of distinct islands. Two cells are part of the same island if they share an edge.You have an array of integers and need to find every pair that sums to a given target. Optimize for time and call out the trade-off you're making.Implement a function that serializes and deserializes a binary tree. Pick the format and explain why it works for your traversal.Given a list of words, group anagrams together. Each group is a list of words; order within groups doesn't matter.Design a rate limiter that allows N requests per user per minute. Implement the check function and discuss how you'd handle a million users.Here's a recursive function that's supposed to flatten a nested list of integers but is throwing stack overflow on large inputs. How would you fix it?Given an array of stock prices indexed by day, return the maximum profit from at most two non-overlapping buy-sell transactions.Implement a trie that supports insert, search, and prefix-search of words. Then write a function that returns all words sharing a given prefix.Write a function that processes a log file and returns the top K most frequent IP addresses. Assume the file is too large to fit in memory.Given a binary tree, return the values you'd see standing on the right side. Each level contributes at most one value.You have a function that finds the kth largest element by sorting. The interviewer asks you to do better — what's your move and what's the worst case?Implement a queue using only stacks. Both enqueue and dequeue should be amortized O(1).Given a directed graph as an adjacency list, detect whether it has a cycle. Return true or false.Implement a function that returns the next available username given a desired one, appending numbers if needed. Multiple workers will call this concurrently — make it safe.Given a 32-bit signed integer, reverse its digits and return the result. If the reversed integer overflows, return zero.Implement a min-stack that supports push, pop, and an O(1) operation that returns the current minimum.Given a long string of words separated by spaces, return the kth most frequent word. If multiple words tie, return the alphabetically smallest.Implement a function that returns whether two strings are one edit away — insert, delete, or replace one character.You're given a function that fetches a user's profile from a remote service. Implement a memoized version that respects a 5-minute freshness window.Given an array, return all distinct triplets whose values sum to zero. The result should not contain duplicate triplets.Design a class that stores a stream of integers and lets callers ask for the number of values within a given range. Inserts and range queries should both be fast.You have an N-by-N grid. Write a function that returns the longest increasing path you can walk through adjacent cells in any direction.Here's a function that's supposed to merge two sorted linked lists into one. It works on most inputs but returns the wrong result when one list is empty. Walk me through your fix.Write a function that returns whether a singly linked list is a palindrome. Optimize for space.Implement a function that takes the root of a binary search tree and returns an iterator that yields values in order. The iterator should use O(h) space.Given a list of words, find all words that can be typed using only one row of a QWERTY keyboard. Define the rows yourself.Write a function that parses an ISO 8601 duration string like 'PT1H30M' and returns total minutes. Handle missing components and call out malformed inputs.You need to find whether two sets of integers have any element in common. The sets can be very large. Walk me through your choice of approach.Given an array of integers and an integer k, return the number of contiguous subarrays whose sum equals k.Implement a function that takes a list of strings and groups them into clusters where strings in the same cluster differ by at most one character.Write a function that takes a JSON-like nested structure and returns all key paths whose value matches a predicate. Don't assume a fixed depth.Given a list of intervals, merge all overlapping ones and return the result sorted by start time.Implement a hash table from scratch with open addressing. Support insert, lookup, and delete with linear probing.You have a method that calls a third-party API for each item in a list. Rewrite it so that calls run concurrently with a maximum of N in flight at any time.Given a string of parentheses, brackets, and braces, return true if they're correctly nested and matched.Implement an exponential backoff helper for an HTTP client. It should accept a callable, a max-retry count, and a base delay, and add jitter.Given an array of integers and a window size k, return the maximum of every contiguous window in a single pass. Sorting each window is too slow — what structure gets you O(n) total?You're given build tasks and their dependencies. Tasks with no unmet dependencies can run in parallel. Write a function that returns the minimum number of rounds needed to finish everything, or reports that it's impossible.Implement an in-memory key-value store that supports get, set, and nested transactions: begin, commit, and rollback. A rollback undoes everything since the matching begin.You have K sorted iterators that are too large to materialize. Write a function that yields all their elements in sorted order, doing O(log K) work per element.Implement a consistent-hashing ring: add a node, remove a node, and look up which node owns a key. Show why only a small fraction of keys move when membership changes.Write a debounce wrapper: given an expensive function and a delay, return a version that only fires after calls stop arriving for that delay. What happens to the arguments and return value of the suppressed calls?You must process N jobs of known sizes within D days, doing jobs in order, one batch per day. Write a function that finds the minimum daily capacity that makes the deadline. What's the search space?Write a function that diffs two nested configuration objects and returns the minimal change set: paths added, removed, and changed. Arrays and scalars can appear at any depth.A binary search implementation returns correct answers on most inputs but loops forever on a few. Without seeing the code, what are the classic mistakes that cause this, and how would you fix the loop invariant?A loop removes items from a list while iterating over it, and users report that some items survive the cleanup. Explain why elements get skipped and give two safe ways to write it.Given an array of integers, move every zero to the end while keeping the relative order of the non-zero elements. Do it in place with O(1) extra space.Design a class that receives a stream of numbers and returns the average of the last k values after each insert. What do you store, and what's the cost per insert?Given two strings, determine whether one is a rotation of the other — for example, 'erbottlewat' is a rotation of 'waterbottle'. Aim for a solution that makes only one containment check.A coding assistant drafted a function that slugifies arbitrary titles into URL-safe strings, and all the tests it wrote for itself pass. What properties and adversarial inputs would you test before trusting it in production?Write a function that takes a sorted array rotated at an unknown pivot and a target value, then returns the index of the target in O(log n) time. If the target doesn't exist, return -1.Implement a circular buffer with fixed capacity that supports enqueue, dequeue, and isFull operations in constant time. Explain how you handle wraparound and discuss thread-safety considerations.Given a directed graph represented as an adjacency list, detect if there's a cycle. Optimize for graphs with millions of nodes and explain your space-time trade-offs.You're given a function that calculates Fibonacci numbers recursively with memoization, but profiling shows it's still slow for n > 10,000. Identify the bottleneck and propose two different optimizations.Design a data structure that supports insert, delete, and getRandom in average O(1) time. All elements have equal probability of being returned by getRandom.Implement a function that merges K sorted linked lists into one sorted list. Analyze the time complexity of your approach and explain when you'd choose an alternative algorithm.Write a function that takes a binary tree and returns true if it's a valid binary search tree. Consider edge cases including duplicate values, integer overflow, and the definition you're using for validity.You have a distributed system that processes financial transactions. Write a test plan for a function that reconciles accounts across three data centers. What test cases matter most and why?Given an array of integers, find the maximum sum of any contiguous subarray. Then extend your solution to handle the case where you can remove at most one element from the chosen subarray.Implement a min-heap that supports standard operations plus a decreaseKey function in O(log n) time. Explain where this data structure is essential and why a balanced BST wouldn't suffice.Design an autocomplete system that returns the top 3 most frequent search queries matching a prefix. Optimize for read-heavy workloads with millions of queries per second and explain your caching strategy.You're reviewing code that uses a hash table to count word frequencies in a document, but it's running out of memory on production logs. Walk through your debugging process and propose fixes.Write a function that determines if one string is a subsequence of another. Then optimize it to handle a stream of queries against a single, fixed parent string.Implement a thread-safe blocking queue with a maximum capacity. Support put, take, and tryTake with timeout. Discuss your choice of synchronization primitives and potential deadlock scenarios.Given a matrix where each row and column is sorted in ascending order, write a function to search for a target value in O(m + n) time. Explain why binary search on each row isn't optimal here.Design a URL shortener that generates unique short codes and redirects users. Handle collisions, discuss how you'd scale to a billion URLs, and explain your choice of character set for codes.Write unit tests for a function that parses and evaluates mathematical expressions with parentheses, addition, subtraction, multiplication, and division. What edge cases would you prioritize?Implement a function that finds the longest palindromic substring in a given string. Compare at least two approaches with different time-space trade-offs and explain when you'd use each.You're given a binary tree where each node has a random pointer that may point to any node in the tree or null. Write a function to deep-copy the tree, preserving all random pointers correctly.Design a logging library that batches writes to disk to minimize I/O. Implement flush policies based on time, buffer size, and log level. Discuss how you'd test durability guarantees without actual disk access.Given a list of time intervals representing busy periods for N people, find all time slots where everyone is free for at least K minutes. Optimize for the common case where most intervals don't overlap.Here's a function that computes edit distance between two strings using dynamic programming, but it's failing on strings longer than 10,000 characters. Debug it and propose both a memory optimization and a way to handle even larger inputs.Implement a consistent hashing ring for distributing cache keys across N servers. Support adding and removing servers with minimal key reassignment. Explain how virtual nodes improve balance.Write a function that takes an array of integers and returns all unique triplets that sum to zero. Optimize to avoid duplicate triplets and analyze how your approach scales with input size.Design a test harness for a payment processing API that must validate idempotency, handle retries correctly, and maintain exactly-once semantics. What failure modes would you simulate and how?