Data Structer And Algorithms Interview Questions

1. What is a Data Structure?

A data structure is a way to organize and store data so it can be used efficiently — like an array, stack, queue, linked list, etc.

2. What is an Algorithm?

An algorithm is a step-by-step procedure or formula to solve a problem.

3. What is the difference between an Array and a Linked List?

ArrayLinked List
Fixed sizeDynamic size
Fast accessSlower access
Insertion/deletion is costlyEasy insertion/deletion

4. What is the Big O notation?

It tells you how fast or slow an algorithm runs as the input grows.

Example:

  • O(1) – constant time
  • O(n) – linear
  • O(log n) – logarithmic
  • O(n²) – quadratic

5. What is a Stack?

A stack is a LIFO (Last In First Out) data structure.

Example: Undo feature, browser history.

stack<int> s; 
s.push(10); s.pop(); 

6. What is a Queue?

A queue is FIFO (First In First Out).

Example: Printer queue, call center.

queue<int> q; 
q.push(5); q.pop(); 

7. What is a Hash Table (Hash Map)?

It stores data as key-value pairs. Fast lookups using a hash function.

unordered_map<string, int> map; 
Map["age"] = 25; 

8. What is Recursion?

A function that calls itself. Best for tree, backtracking problems.

Example:

int factorial(int n) {     
    if(n == 0) return 1;     
    return n * factorial(n-1); 
} 

9. What is a Binary Tree?

A tree where each node has max 2 children (left and right).

10. What is a Binary Search Tree (BST)?

A binary tree where left child < node < right child.

Used for fast searching, insertion, and deletion.

11. What is Tree Traversal?

  • Inorder (Left, Root, Right)
  • Preorder (Root, Left, Right)
  • Postorder (Left, Right, Root)

12. What is a Linked List?

A sequence of nodes where each node points to the next.

Types:

  • Singly
  • Doubly
  • Circular

13. What is a Graph?

A collection of nodes (vertices) connected by edges.

Used in Google Maps, Social Media.

14. What is DFS and BFS?

DFS (Depth First Search) – Go deep first.

BFS (Breadth First Search) – Go level by level.

15. What is a Heap?

A special tree used to get the min or max in O(1) time.

Used in priority queues.

17. What is Dynamic Programming (DP)?

Breaking problems into smaller overlapping subproblems and storing their results.

Example: Fibonacci with memoization.

18. What is the Two-pointer technique?

Use two indexes to solve problems efficiently like:

  • Pair sum
  • Reverse string
  • Remove duplicates

19. What are some common sorting algorithms?

  • Bubble Sort – O(n²)
  • Selection Sort – O(n²)
  • Insertion Sort – O(n²)
  • Merge Sort – O(n log n)
  • Quick Sort – O(n log n)
  • Heap Sort – O(n log n)

20. What is the Sliding Window technique?

Used for problems involving subarrays or substrings.

Example: Find max sum of a subarray of size k in an array.