Data Structures & Algorithms interview questions and quick answers
Arrays, linked lists, hashing, trees and patterns, with line-by-line code and complexity.
Review 671 questions and short answers on Data Structures & Algorithms. Start with real examples below, then search, practise and follow links to full explanations.
Data Structures & Algorithms: questions worth revising first
These answers come from AI Tapri's existing question bank and published interview guides. Open each answer and read the linked in-depth guide for examples and context.
01What is the difference between an array and a linked list, and when would you choose each?
An array stores items contiguously, so indexing is O(1) but inserting in the middle is O(n). A linked list stores nodes connected by pointers, so insertion at a known node is O(1) but reaching the kth item is O(n).
Read the full explanation02How do you find the maximum profit from buying and selling a stock once?
Scan once while tracking the lowest price so far. At each day, the best profit if you sell today is price minus that lowest price. Keep the maximum of those.
Read the full explanation03What is the difference between BFS and DFS, and when would you use each?
BFS (breadth-first search) explores a graph level by level using a queue, so it finds the shortest path in an unweighted graph. DFS (depth-first search) goes as deep as possible along one path before backtracking, using a stack or recursion, and suits cycle detection, topological sort and exploring all paths. Both take O(V + E) time.
Read the full explanation04How do you implement binary search, and what are the common bugs?
On a sorted array, keep two pointers low and high, check the middle element, and throw away the half that cannot contain the target. Repeat until found or low passes high. It runs in O(log n). The classic bugs are computing mid as (low + high) / 2, which can overflow in Java or C++, and off-by-one errors in the loop condition and pointer updates.
Read the full explanation05How do you check if one string is a rotation of another?
They must have the same length, and the second string must appear inside the first string written twice. For example, "waterbottle" doubled contains "erbottlewat".
Read the full explanation06How do you check whether two strings are anagrams of each other?
Two strings are anagrams if they have the same letters with the same counts. Count the characters of the first string, subtract using the second, and check that every count ends at zero.
Read the full explanation07What is a common pitfall related to "What is the difference between an array and a linked list, and when would you choose each"?
Saying linked list insertion is always O(1): finding the position is still O(n).
Read the full explanation08How would you explain "What is the difference between an array and a linked list, and when would you choose each" in an interview?
"Arrays give O(1) random access but costly middle inserts; linked lists give O(1) inserts once you hold the node but O(n) lookup and extra memory for pointers. I pick an array when I read by index a lot and a linked list when I insert and delete at the ends or at known nodes often."
Read the full explanationFind your next answer.
Pick a topic, search a keyword, or open a question to reveal the answer.
Data Structures & Algorithms quick answers
Free to practise. No account needed. Made for quick revision, not rote memorisation.
Keep preparing for Data Structures & Algorithms
Quick answers help with recall; detailed guides explain the reasoning, examples and common mistakes behind each answer.