Data Structures and Algorithms Interview Questions & Answers
All 200 unique questions in one place — 60 easy, 95 medium, 45 hard — grouped by topic.
Practise by topic
Practise the way your interview works
What is the difference between Big-O, Big-Omega and Big-Theta?
Show answer
What is space complexity and how is auxiliary space different?
Show answer
List common complexity classes from fastest to slowest with examples.
Show answer
What is recursion and why is a base case essential?
Show answer
Write recursive Fibonacci and then optimise it with memoisation.
Show answer
Solve the Tower of Hanoi problem recursively.
Show answer
Find the GCD of two numbers using the Euclidean algorithm.
Show answer
How do you approach a coding problem you have never seen before in an interview?
Show answer
What is amortised analysis? Explain it with dynamic array append.
Show answer
What are the trade-offs between recursion and iteration?
Show answer
What is tail recursion and do Java and Python optimise it?
Show answer
Why is naive recursive Fibonacci exponential, and how do you fix it?
Show answer
Explain best, average and worst case complexity using quicksort.
Show answer
A problem has n up to 100000. Which time complexities are acceptable for a 1-second limit?
Show answer
A recursive DFS in Python crashes with RecursionError on a deep input. How do you fix it?
Show answer
An interviewer asks you to reduce your solution's extra space from O(n) to O(1). How do you approach it?
Show answer
Implement fast exponentiation to compute x to the power n in O(log n).
Show answer
Generate all subsets of a list of distinct numbers.
Show answer
What is the time complexity of an outer loop from 0 to n with an inner loop where j starts at 1 and doubles until it reaches n?
Show answer
How did you prepare for data structures and algorithms rounds?
Show answer
What is the Master Theorem and how do you apply it?
Show answer
Your solution passes the sample tests but gets Time Limit Exceeded on large inputs. How do you analyse and fix it?
Show answer
You can use an O(n log n) algorithm with high constant factors or a simple O(n squared) one. For what inputs might the O(n squared) choice be better?
Show answer
A report generation function loops over all users and, for each user, scans all orders. It was fine at launch but is now very slow. How do you explain and fix it?
Show answer
What is the difference between a static array and a dynamic array?
Show answer
What is the two pointer technique and when is it used?
Show answer
Strings are immutable in Java and Python. How does that affect algorithm complexity?
Show answer
What is the difference between a subarray, a subsequence and a substring?
Show answer
How would you check whether two strings are anagrams, and what changes if input can contain any Unicode characters?
Show answer
Solve two sum: return the indices of two numbers that add up to the target.
Show answer
Implement Kadane's algorithm for the maximum subarray sum.
Show answer
Check whether a string is a palindrome, ignoring case and non-alphanumeric characters.
Show answer
Describe a time you got stuck during a coding interview or contest. How did you recover?
Show answer
Explain the sliding window technique with fixed and variable windows.
Show answer
What are prefix sums and what problems do they solve?
Show answer
How does a hash table work, and how are collisions handled?
Show answer
What is Kadane's algorithm?
Show answer
What is the load factor of a hash table and why does it matter?
Show answer
What is the Dutch National Flag algorithm?
Show answer
An array of size 100000 must answer 100000 queries asking for the sum between two indices. What approach would you use?
Show answer
Your brute-force solution for the longest substring without repeating characters is O(n cubed). How would you improve it?
Show answer
You must merge two sorted arrays where the first array has enough empty space at the end for the second. How would you do it in place?
Show answer
Find the length of the longest substring without repeating characters.
Show answer
Rotate an array to the right by k steps in place.
Show answer
Tell me about a time you used the right data structure to solve a real problem in a project or job.
Show answer
How do the KMP and Rabin-Karp string matching algorithms work?
Show answer
You have a 100 GB file of integers and only 4 GB of RAM. How would you find the duplicates?
Show answer
A hash map lookup in your service suddenly becomes very slow under certain traffic. What could cause it?
Show answer
Return an array where each element is the product of all other elements, without using division.
Show answer
What is the difference between a linked list and an array?
Show answer
Compare singly, doubly and circular linked lists.
Show answer
What is a stack and where is it used?
Show answer
What are a queue, a deque and a circular queue?
Show answer
How would you implement browser back and forward navigation?
Show answer
Users enter formulas with brackets in a spreadsheet tool. How do you validate that the brackets are balanced?
Show answer
Merge two sorted linked lists into one sorted list.
Show answer
Check whether a string of brackets is valid.
Show answer
How does Floyd's cycle detection algorithm work and why is it correct?
Show answer
What is a monotonic stack and which problems does it solve?
Show answer
How can you implement a queue using two stacks, and what is its complexity?
Show answer
What is a dummy or sentinel node and why is it useful in linked list problems?
Show answer
You are building undo and redo for a text editor. Which data structures would you use?
Show answer
Design a simple scheduler that gives each job a fixed time slice in turn.
Show answer
A trading application needs a stack that also returns the current minimum in O(1). How would you design it?
Show answer
Detect whether a linked list has a cycle and return the node where the cycle begins.
Show answer
Find the next greater element for every element of an array.
Show answer
Tell me about a time you explained a technical idea to a non-technical person.
Show answer
Describe a team project where one member was not contributing. What did you do?
Show answer
How does a monotonic deque find the maximum of every sliding window in O(n)?
Show answer
Why are linked lists often slower than arrays in practice, and what is a skip list?
Show answer
Two linked lists merge at some node and share the rest. How do you find the merge point efficiently?
Show answer
Design a rate limiter that allows at most 100 requests per user in any rolling 60-second window.
Show answer
Reverse a linked list in groups of k nodes.
Show answer
Tell me about a time you received critical feedback on your code.
Show answer
Explain basic binary tree terminology: root, leaf, height and depth.
Show answer
What are inorder, preorder, postorder and level order traversals?
Show answer
What is a binary search tree and what is its time complexity?
Show answer
What is the difference between full, complete and perfect binary trees?
Show answer
Write an iterative inorder traversal of a binary tree.
Show answer
Find the maximum depth of a binary tree.
Show answer
Tell me about the project you are most proud of.
Show answer
Compare AVL trees and red-black trees.
Show answer
What is a trie and where is it used?
Show answer
How do you find the lowest common ancestor in a BST versus in a general binary tree?
Show answer
How do DFS and BFS differ in memory use on a tree?
Show answer
What is the diameter of a binary tree and how do you compute it efficiently?
Show answer
You must build autocomplete for a search box over one million product names. Which data structure would you use?
Show answer
An application needs sorted data with frequent inserts, deletes and range queries such as all orders between two timestamps. Which structure fits?
Show answer
A teammate's BST validation only checks that each node is greater than its left child and smaller than its right child. Why is that wrong?
Show answer
In a company org chart stored as a tree, how would you find the closest common manager of two employees?
Show answer
Print the level order traversal of a binary tree, one list per level.
Show answer
Write a function to validate a binary search tree.
Show answer
Find the lowest common ancestor of two nodes in a binary tree.
Show answer
Tell me about an interview or exam you failed and what you learned.
Show answer
Compare segment trees and Fenwick trees.
Show answer
What is a B-tree and why do databases use it for indexes?
Show answer
A recursive tree traversal crashes on a skewed tree with one million nodes. How do you fix it?
Show answer
A game leaderboard with millions of players needs fast score updates and queries like what rank is this player. How would you design it?
Show answer
Serialize a binary tree to a string and deserialize it back.
Show answer
What is a heap and how is it stored in an array?
Show answer
How do you use a priority queue in Java and Python?
Show answer
A cashier system uses the greedy rule of always giving the largest coin first. When can this fail?
Show answer
Find the kth largest element in an array using a heap.
Show answer
Tell me about a time you had to make a decision with incomplete information.
Show answer
Why do you want to work at a product company as a software engineer?
Show answer
What are the complexities of heap operations, and why does building a heap take O(n)?
Show answer
How does heap sort work and how does it compare with quicksort and merge sort?
Show answer
What is a greedy algorithm and when does it give the optimal answer?
Show answer
How do you decide between a greedy approach and dynamic programming?
Show answer
In activity selection, why do we sort by end time rather than start time or duration?
Show answer
Why does greedy work for fractional knapsack but fail for 0/1 knapsack?
Show answer
How can two heaps maintain the running median of a stream?
Show answer
Given meeting start and end times for a day, how do you find the minimum number of rooms needed?
Show answer
A hospital emergency system must treat patients by severity, and patients with equal severity in arrival order. How would you implement it?
Show answer
Merge K sorted linked lists using a min-heap.
Show answer
Find the minimum number of railway platforms needed given arrival and departure times.
Show answer
Solve jump game: can you reach the last index if nums[i] is the maximum jump from i?
Show answer
How do you prioritise when several tasks have deadlines at the same time?
Show answer
How does Huffman coding work?
Show answer
How do you prove that a greedy algorithm is correct?
Show answer
You need to merge K sorted log files from different servers into one sorted output. How would you do it efficiently?
Show answer
A monitoring service must report the median response time over a live stream of requests. How would you design it?
Show answer
CPU tasks labelled by type must be scheduled with a cooldown of n slots between two tasks of the same type. How do you minimise total time?
Show answer
Implement a data structure that returns the median of all numbers added so far.
Show answer
What are the ways to represent a graph and when would you use each?
Show answer
What is the difference between BFS and DFS, and when do you use each?
Show answer
Explain directed, undirected, weighted graphs and DAGs.
Show answer
A metro app must find the route with the fewest stations between two stops. Which algorithm would you use?
Show answer
A satellite image is a grid of land and water cells. How do you count the islands?
Show answer
Count the number of islands in a grid of 1s and 0s.
Show answer
Where do you see yourself in five years?
Show answer
How does Dijkstra's algorithm work and why does it fail with negative edge weights?
Show answer
What is topological sorting and how is it done?
Show answer
What is the Union-Find data structure and what optimisations make it fast?
Show answer
Compare Kruskal's and Prim's algorithms for minimum spanning trees.
Show answer
What is a bipartite graph and how do you check for one?
Show answer
A learning platform has courses with prerequisites. How do you find a valid order, and what if none exists?
Show answer
How would you generate friend suggestions as friends of friends in a social network?
Show answer
A build system hangs because some services depend on each other in a loop. How would you detect and report the cycle?
Show answer
Find the shortest path length from the top-left to the bottom-right of a grid where 1 is a wall.
Show answer
Implement Dijkstra's algorithm with a priority queue.
Show answer
Implement topological sort using Kahn's algorithm.
Show answer
Tell me about a time you worked with people from other teams to solve a complex problem.
Show answer
How do you handle disagreement with a reviewer about your solution in a code review?
Show answer
Compare Bellman-Ford and Floyd-Warshall.
Show answer
How does cycle detection differ in directed and undirected graphs?
Show answer
Find the cheapest flight from one city to another using at most K stops. Why does plain Dijkstra not work directly?
Show answer
A network has 10 million devices and connections are added continuously. You must answer whether two devices are connected in real time. How?
Show answer
Implement Union-Find with path compression and union by size.
Show answer
What is dynamic programming, and what are overlapping subproblems and optimal substructure?
Show answer
What is the difference between memoisation and tabulation?
Show answer
A warehouse robot moves only right or down on a grid with some blocked cells. How do you count the number of paths?
Show answer
Solve climbing stairs: how many ways are there to climb n steps taking 1 or 2 steps at a time?
Show answer
Solve house robber: maximise money robbed from houses in a row without robbing two adjacent houses.
Show answer
Tell me about a time you kept working on a hard problem when others had given up.
Show answer
How do you recognise a DP problem and define its state?
Show answer
Explain the recurrence for the 0/1 knapsack problem.
Show answer
How is the longest common subsequence computed with DP?
Show answer
How can you reduce the space used by a DP solution?
Show answer
What is edit distance and where is it used?
Show answer
Your recursive solution passes with memoisation, but the interviewer asks for a bottom-up version. How do you convert it?
Show answer
A search box must suggest the closest dictionary words when a user makes a typo. How would you approach it?
Show answer
You need to build a tool that shows the differences between two versions of a file line by line. What algorithm would you use?
Show answer
Find the minimum number of coins needed to make an amount.
Show answer
Compute the length of the longest common subsequence of two strings.
Show answer
Implement 0/1 knapsack using a one-dimensional DP array.
Show answer
How do you handle pressure during a timed online coding test?
Show answer
What is bitmask DP and when is it used?
Show answer
What is DP on trees? Give an example.
Show answer
What is interval DP? Explain with matrix chain multiplication.
Show answer
How do you find the longest increasing subsequence in O(n log n)?
Show answer
A company has a fixed budget and several projects with costs and expected returns. How would you choose projects to maximise return?
Show answer
Your knapsack DP has n equal to 100 items but capacity up to 10 to the power 9, so the table does not fit in memory. What do you do?
Show answer
Compute the edit distance between two words.
Show answer
How does binary search work and what does it require?
Show answer
What is a stable sort and why does stability matter?
Show answer
What is backtracking?
Show answer
A bug appeared somewhere in the last 1000 commits. How would you find the commit that introduced it quickly?
Show answer
HR wants an employee list sorted by department, and by salary within each department. How would you do it?
Show answer
Write an iterative binary search that returns the index of a target or -1.
Show answer
Compute the integer square root of a number using binary search.
Show answer
Tell me about yourself for a software engineer role.
Show answer
How does merge sort work and what are its properties?
Show answer
How does quicksort work and why does pivot choice matter?
Show answer
What are lower bound and upper bound, and what common bugs appear in binary search?
Show answer
Packages with given weights must be shipped in order within D days. How do you find the minimum ship capacity?
Show answer
A sorted array has been rotated at an unknown point. How can you still search it in O(log n)?
Show answer
Implement merge sort.
Show answer
Find the first and last position of a target in a sorted array.
Show answer
Generate all permutations of a list of distinct numbers using backtracking.
Show answer
Why are you looking for a change from your current job?
Show answer
Why can comparison sorts not beat O(n log n), and how do counting and radix sort get around it?
Show answer
What does binary search on the answer mean?
Show answer
What is Timsort and why do Python and Java use it?
Show answer
What is quickselect and when is it better than sorting?
Show answer
You must sort a 10 GB file of records on a machine with 1 GB of RAM. How would you do it?
Show answer
A Sudoku solver using plain backtracking is too slow on hard puzzles. How would you speed it up?
Show answer
Search for a target in a rotated sorted array.
Show answer
Tell me about a time you made slow code significantly faster at work.
Show answer
Frequently asked questions
Why is DSA important for getting a software job in India?
Most companies use online coding tests and DSA interview rounds as the main filter for software roles, from mass campus hiring at IT services firms to SDE roles at product companies, fintechs and trading firms. DSA shows how well you think, break down problems and write efficient code. Strong DSA skills often decide whether you get a standard offer or a higher-paying digital or product role.
Which programming language should I use for DSA?
DSA is language-neutral, so choose one language you are comfortable with and stick to it. Java and Python are the most common choices in India; Python is concise and quick to write, while Java and C++ are faster and widely used in competitive programming. Interviewers care about your logic, complexity analysis and clean code far more than the language, as long as you know its standard collections well.
Who can learn this course and what are the prerequisites?
Students of B.Tech, BCA, MCA, B.Sc and diploma programmes, as well as working professionals preparing for job switches, can take this course. You need basic programming knowledge in any one language, including loops, functions and arrays, and school-level mathematics. If you are completely new to programming, spend two to three weeks learning Java or Python basics first, and then start the DSA curriculum.
How long does it take to become interview-ready in DSA?
With two to three hours of focused practice daily, most learners become comfortable with core patterns in about three to four months, solving around 250 to 350 well-chosen problems. Product company interviews may need more practice on graphs and dynamic programming. Consistency matters more than speed, so solve problems by pattern, revise mistakes regularly and take timed contests every week.
How many problems should I solve, and are platforms like LeetCode enough?
Quality matters more than count. Around 300 problems covering all major patterns, with a mix of easy, medium and some hard, is enough for most interviews if you understand each solution deeply. Platforms like LeetCode, HackerRank and Codeforces provide good practice, but you also need structured learning of concepts, regular revision, mock interviews and practice explaining your approach aloud.
Will I get a certificate, and what else helps me get hired?
You receive a course completion certificate after finishing the curriculum and assessments. It shows structured learning, but hiring decisions depend mainly on your performance in coding tests and interviews. Maintain a GitHub repository of solved problems organised by pattern, build two or three projects that use data structures meaningfully, keep active profiles on coding platforms and practise mock interviews to convert opportunities into offers.