📚 Data Structures & Algorithms Interview Questions & Answers (2025)
Basic Level Questions
▶
What is a data structure?A data structure is a way to organize, manage, and store data for efficient access and modification.
▶
What is the difference between an array and a linked list?Arrays use contiguous memory for fast random access but have fixed size; linked lists use nodes connected by pointers allowing dynamic size but slower access.
▶
What is a stack and how does it work?A stack is a LIFO (Last-In-First-Out) data structure where elements are added using push and removed using pop operations. Commonly used in function calls and undo operations.
▶
What is a queue?A queue is a FIFO (First-In-First-Out) data structure where elements are added using enqueue and removed using dequeue operations. Used in CPU scheduling, printers, etc.
▶
What is a linked list?A linked list is a sequence of nodes where each node contains data and a reference (pointer) to the next node. Variants include singly, doubly, and circular linked lists.
▶
What are linear and nonlinear data structures?Linear data structures arrange elements sequentially (e.g., array, stack, queue), while nonlinear data structures organize data hierarchically or in networks (e.g., tree, graph).
▶
What operations can be performed on arrays?Common operations include insertion, deletion, searching, traversal, updating, and sorting elements.
▶
What is a dynamic array?A dynamic array automatically resizes when capacity is exceeded; examples include ArrayList in Java and Vector in C++.
▶
What are the advantages of linked lists over arrays?Linked lists offer dynamic sizing and efficient insertions/deletions but have no random access and use more memory due to pointers.
▶
What is a circular queue?A circular queue connects the last position back to the first, efficiently utilizing space by avoiding shifting of elements.
Intermediate Level Questions
▶
What is a binary tree?A hierarchical data structure in which each node has at most two children referred to as left and right.
▶
What is a binary search tree (BST)?A BST is a binary tree where the left child is less than the node and the right child is greater, enabling efficient searching, insertion, and deletion.
▶
Explain different tree traversals.Common traversals include Inorder (Left-Node-Right), Preorder (Node-Left-Right), Postorder (Left-Right-Node), and Level Order (Breadth-First Search).
▶
What is recursion? Give an example.Recursion is the process where a function calls itself; for example, computing factorial(n) recursively.
▶
What is hashing? How are collisions handled?Hashing maps keys to array indices. Collisions occur when two keys map to the same index and are typically handled by chaining or open addressing.
▶
Explain depth-first search (DFS) and breadth-first search (BFS).DFS explores as far as possible along a branch before backtracking; BFS explores all neighbors at a given depth level before moving deeper.
▶
What is a heap? Types?A heap is a complete binary tree used as max-heap (parent ≥ children) or min-heap (parent ≤ children), commonly used in priority queues.
▶
What is dynamic programming?A method for solving complex problems by breaking them into overlapping subproblems and caching results (memoization).
▶
What is a graph? Types of graphs?A graph is a set of vertices and edges. Types include directed, undirected, weighted, unweighted, cyclic, and acyclic graphs.
▶
What is a hash table?A data structure providing efficient key-value pair storage, using hashing for fast insertion, deletion, and lookups.
▶
Explain the time complexity of common sorting algorithms.Examples: Bubble Sort O(n²), Merge Sort O(n log n), Quick Sort average O(n log n), worst O(n²).
▶
What is the difference between BFS and DFS in graph traversal?BFS explores neighbors level-wise, useful for shortest path; DFS explores depth-wise, which can use less memory.
▶
How do you detect cycles in a graph?Use DFS with recursion stack for directed graphs or union-find for undirected graphs.
▶
What is a trie?A trie is a tree-like data structure used to store dynamic sets of strings, useful for autocomplete and spell checking.
▶
What is amortized analysis?Average cost per operation over a sequence, smoothing out costly operations. Example: dynamic array resizing.
▶
Describe topological sorting.A linear ordering of vertices in a DAG where for every directed edge u→v, u comes before v.
▶
What is Floyd’s cycle detection algorithm?Also known as Tortoise and Hare algorithm, detects cycles in linked lists using two pointers moving at different speeds.
▶
Explain segment trees.Data structure to efficiently perform range queries and updates on arrays.
▶
What is recursion stack overflow?Occurs when recursive function calls exceed stack size limit, causing program crash.
▶
What is the Master Theorem?A method to determine the time complexity of divide-and-conquer algorithms.
▶
What are disjoint sets?Data structure to keep track of partitions of a set, supporting union and find operations efficiently.
Advanced Level Questions
▶
What are balanced binary trees? Examples?Balanced trees maintain height difference ≤ 1 between subtrees to ensure operations remain O(log n). Examples include AVL and Red-Black trees.
▶
Explain Dijkstra’s algorithm.An algorithm to find the shortest path from a source to all vertices in a weighted graph with non-negative weights.
▶
What is the difference between Prim’s and Kruskal’s algorithms?Both find minimum spanning trees; Prim’s grows from a node adding edges; Kruskal’s sorts all edges and adds them if they don’t form cycles.
▶
Describe graph representation methods.Graphs can be represented using adjacency matrices or adjacency lists based on space-time tradeoffs.
▶
What is the significance of caching?Caching stores frequent results to reduce computation time and improve algorithm performance.
▶
Explain amortized analysis with examples.Analyzes average time per operation over a sequence, e.g., array resizing: some costly but average remains O(1).
▶
How to implement an LRU cache?Use a hashmap and a doubly linked list to allow O(1) access and eviction of least recently used elements.
▶
Explain the difference between greedy algorithms and dynamic programming.Greedy algorithms make local optimal choices; dynamic programming solves overlapping subproblems and uses memoization for optimal solutions.
▶
What is the difference between Divide and Conquer and Dynamic Programming?Divide and Conquer breaks problems into independent subproblems; Dynamic Programming solves overlapping subproblems with memoization.
▶
Explain suffix trees and their applications.Suffix trees are compressed tries for all suffixes of a string, used in pattern matching and bioinformatics.
▶
Describe backtracking with an example.A recursion-based approach for building solutions by trying and reverting choices. Example: solving Sudoku or N-Queens problems.
▶
What is the Bellman-Ford algorithm?An algorithm to compute shortest paths from a source vertex to all vertices in a graph, handling negative weights.
▶
Explain heap data structure and common uses.A heap is a specialized tree-based structure that satisfies heap property, used in priority queues and heap sort.
▶
What are tries? Explain one usage.Tries are prefix trees used for efficient searching and autocomplete in dictionaries and IP routing.
▶
Explain Floyd-Warshall algorithm.An algorithm for finding shortest paths between all pairs of vertices in a weighted graph.
▶
What is the significance of Big-O notation?Used to classify algorithms by their runtime or space requirements as input size grows.
▶
What is a bloom filter?A space-efficient probabilistic data structure to test whether an element is a member of a set with false positives allowed.
▶
How does Rabin-Karp algorithm work?A string-searching algorithm using hashing to find patterns efficiently with average O(n) complexity.
▶
Explain the concept of hashing with perfect hash functions.Hash functions that map distinct keys to distinct indices with no collisions, ideal for static datasets.
▶
What are suffix arrays and their advantage over suffix trees?Suffix arrays are space-efficient alternatives to suffix trees for substring search, allowing binary search-based queries.
▶
Explain space complexity and why it matters.Measures memory required by an algorithm; critical for resource-constrained systems and performance optimization.