Krazy Kaksha

Interview Prep

Top 40 DSA Interview Questions and Answers for Freshers (2026)

Krazy Kaksha 7 min read

Top 40 DSA Interview Questions and Answers for Freshers (2026)

Data Structures and Algorithms (DSA) is the one topic every tech company tests, from service companies to product companies. In campus rounds the questions are not very hard, but they repeat a lot. Below are the 40 most asked DSA questions for freshers with short answers you can say in an interview, plus the time complexity table you must remember.

Learn the idea first, then the answer. Interviewers always ask “why” after “what”.

Quick summary

SectionQuestionsFocus
Basics and complexityQ1 to Q6Big O, time vs space
Arrays and stringsQ7 to Q13Two pointers, sliding window
Linked list, stack, queueQ14 to Q22Pointers, LIFO, FIFO
Trees and graphsQ23 to Q31Traversals, BST, BFS, DFS
Sorting, searching, hashingQ32 to Q36Merge, quick, binary search
Recursion and DPQ37 to Q40Base case, memoization

Basics and complexity

1. What is a data structure?
A way to store and organise data so it can be used efficiently. Examples: array, linked list, stack, queue, tree, graph, hash table.

2. Linear vs non-linear data structure?
Linear: elements in a sequence (array, linked list, stack, queue). Non-linear: elements in a hierarchy or network (tree, graph).

3. What is time complexity?
How the running time grows with input size n, written in Big O. Example: a single loop is O(n), a nested loop is O(n²).

4. What is space complexity?
Extra memory the algorithm needs as n grows. Recursion also uses stack space.

5. What do Big O, Omega and Theta mean?
Big O = upper bound (worst case), Omega = lower bound (best case), Theta = tight bound.

6. Which is faster: O(n log n) or O(n²)?
O(n log n). For n = 1,000,000, n² is a trillion steps, n log n is about 20 million.

Arrays and strings

7. Array vs linked list?
Array: fixed size, contiguous memory, O(1) access by index, costly insert/delete. Linked list: dynamic size, O(n) access, O(1) insert/delete at a known node.

8. How do you reverse an array?
Two pointers at start and end, swap and move inward. O(n) time, O(1) space.

9. Find the second largest element in one pass.
Keep two variables, first and second; update them while scanning. O(n).

10. What is the two pointer technique?
Two indexes moving through the data (from both ends or same direction) to avoid nested loops. Used in pair sum on a sorted array, removing duplicates, palindrome check.

11. What is the sliding window technique?
Keep a window of elements and slide it instead of recalculating. Used for maximum sum subarray of size k, longest substring without repeating characters.

12. Kadane’s algorithm?
Finds the maximum subarray sum in O(n): keep a running sum, reset it to 0 when it goes negative, track the best.

13. How do you check if two strings are anagrams?
Count characters with a hash map or a 26 size array and compare. O(n).

Linked list, stack and queue

14. Types of linked list?
Singly, doubly and circular linked list.

15. How do you reverse a linked list?
Use three pointers: prev, curr, next. Point curr.next to prev and move forward. O(n), O(1) space.

16. How do you detect a loop in a linked list?
Floyd’s cycle detection: slow pointer moves 1 step, fast moves 2. If they meet, there is a loop.

17. Find the middle of a linked list.
Slow and fast pointers again; when fast reaches the end, slow is at the middle.

18. What is a stack?
LIFO (Last In First Out). Operations push, pop, peek, all O(1). Used in function calls, undo, expression evaluation.

19. What is a queue?
FIFO (First In First Out). Operations enqueue, dequeue. Used in BFS, printers, CPU scheduling.

20. Check balanced parentheses.
Push opening brackets on a stack; for each closing bracket, the top must match. Stack empty at the end means balanced.

21. Implement a queue using two stacks.
Push into stack 1. For dequeue, if stack 2 is empty move everything from stack 1 to stack 2, then pop stack 2.

22. What is a circular queue and a deque?
Circular queue reuses empty space by wrapping the rear to the front. Deque allows insert and delete at both ends.

Trees and graphs

23. What is a binary tree?
A tree where each node has at most two children, left and right.

24. What is a Binary Search Tree (BST)?
Left subtree values are smaller, right subtree values are larger. Search, insert, delete are O(log n) on average, O(n) if skewed.

25. Tree traversals?
Inorder (left, root, right), Preorder (root, left, right), Postorder (left, right, root), Level order (BFS with a queue). Inorder of a BST gives sorted order.

26. Height of a binary tree?
1 + max(height of left, height of right), with height of null = 0. Recursive, O(n).

27. What is a balanced tree?
Heights of left and right subtrees differ by at most 1 at every node. AVL and Red-Black trees keep themselves balanced.

28. What is a heap?
A complete binary tree where every parent is smaller (min heap) or larger (max heap) than its children. Used for priority queues and finding top k elements.

29. What is a graph and how is it stored?
Nodes (vertices) connected by edges. Stored as an adjacency matrix (O(V²) space) or adjacency list (O(V + E) space).

30. BFS vs DFS?
BFS goes level by level using a queue and finds the shortest path in unweighted graphs. DFS goes deep first using a stack or recursion, used for cycle detection and topological sort.

31. What is Dijkstra’s algorithm?
Finds the shortest path from one source in a graph with non-negative weights, using a min heap. O((V + E) log V).

Sorting, searching and hashing

32. Binary search?
Works on a sorted array: check the middle, then search the left or right half. O(log n).

33. Merge sort vs quick sort?
Merge sort: always O(n log n), stable, needs O(n) extra space. Quick sort: O(n log n) average, O(n²) worst, in-place, usually faster in practice.

34. Which sorting is stable?
Merge sort, insertion sort and bubble sort are stable. Quick sort and heap sort are not.

35. What is hashing?
Mapping a key to an index using a hash function so search, insert and delete take O(1) on average.

36. What is a collision and how is it handled?
Two keys get the same index. Fixed by chaining (linked list at each index) or open addressing (linear or quadratic probing).

Recursion and dynamic programming

37. What is recursion?
A function calling itself with a smaller input until a base case. Without a base case you get stack overflow.

38. What is dynamic programming?
Solving a problem by breaking it into overlapping subproblems and storing their answers so each is solved once.

39. Memoization vs tabulation?
Memoization: top-down recursion with a cache. Tabulation: bottom-up filling of a table.

40. Name classic DP problems.
Fibonacci, 0/1 knapsack, longest common subsequence, coin change, longest increasing subsequence.

Time complexity cheat sheet

OperationArrayLinked listBST (avg)Hash table (avg)
AccessO(1)O(n)O(log n)O(1)
SearchO(n)O(n)O(log n)O(1)
InsertO(n)O(1)O(log n)O(1)
DeleteO(n)O(1)O(log n)O(1)
SortingBestAverageWorstSpace
BubbleO(n)O(n²)O(n²)O(1)
InsertionO(n)O(n²)O(n²)O(1)
MergeO(n log n)O(n log n)O(n log n)O(n)
QuickO(n log n)O(n log n)O(n²)O(log n)
HeapO(n log n)O(n log n)O(n log n)O(1)

How to prepare in 30 days

  • Week 1: arrays, strings, two pointers, sliding window.
  • Week 2: linked list, stack, queue, hashing.
  • Week 3: trees, BST, heaps, graphs (BFS, DFS).
  • Week 4: recursion, DP basics and mock interviews.
  • Solve 3 to 5 problems a day and explain each one aloud, as you would in an interview.

Keep preparing

Save this page and revise the cheat sheet the night before your interview.

Doubts? Ask here

Leave a comment

Your email is not shown. Comments appear after a quick check.