Welcome to Interviewbit, help us create the best experience for you!

Currently, You are a:

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your education

College/University *
Enter the name of your college
Branch *
Year of completion *

Few details about your career...

Current Company *
Enter company name
Experience *

You're all set!

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Data Structure MCQ With Answers

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Data Structures are entities used in programming, which can store some form of data in some ordered form, which allows us to perform some efficient processing on them. The efficient processing can be in terms of time, space, or both, or it can be based on some other factor as a priority that is needed for some specific problem.

Data structures can be divided into 2 types:

  • Primitive Data Structures: int, bool, float, etc.
  • Abstract Data Structures: Tree, LinkedList, etc.

In terms of memory representation and structure, it can also be classified into 2 types:

  • Linear Data Structures: Arrays.
  • Non-Linear Data Structures: LinkedList, Trees, etc.

Algorithms are some well-defined set of instructions, steps, or logic, written such that by following the steps, some specific task is accomplished. It is not the working program in itself, but rather a well-defined series of steps, encompassing the program’s underlying logic, following which the working program can be coded down. Algorithms must satisfy the following properties:

  • Input: There should be >= 0 inputs given to the algorithm to work on.
  • Output: The algorithm must provide some form of output.
  • Finiteness: The algorithm should have a finite number of steps.
  • Definiteness: Every step of the algorithm should be well-defined and not be ambiguous.
  • Correctness: The algorithm must provide a correct output.

The efficiency of the algorithm is also measured on various parameters, the most important of them being:

  • Time Complexity.
  • Space Complexity.

Algorithms are varied and vast, and every new program can be classified into a different category of algorithms, but some of the famous used examples are Flows, Lowest Common Ancestor of Nodes, Sorting, Searching, etc.

Data Structure MCQ

1. 

How is an array initialized in C language?

2. 

Which of the following is a linear data structure?

3. 

How is the 2nd element in an array accessed based on pointer notation?

4. 

What will be the output of the following code snippet?

void solve() {
   int a[] = {1, 2, 3, 4, 5};
   int sum = 0;
   for(int i = 0; i < 5; i++) {
       if(i % 2 == 0) {
           sum += a[i];
       }
   }
   cout << sum << endl;
}
5. 

What will the output of the following code snippet?

void solve() {
   int a[] = {1, 2, 3, 4, 5};
   int sum = 0;
   for(int i = 0; i < 5; i++) {
       if(i % 2 == 0) {
           sum += *(a + i);
       }
       else {
           sum -= *(a + i);
       }
   }
   cout << sum << endl;
}
6. 

How are String represented in memory in C?

7. 

What function is used to append a character at the back of a string in C++?

8. 

What is the time complexity of the following code snippet in C++?

void solve() {
   string s = "scaler";
   int n = s.size();
   for(int i = 0; i < n; i++) {
       s = s + s[i];
   }
   cout << s << endl;
}
9. 

What is the disadvantage of array data structure?

10. 

What is the output of the following code snippet?

void solve() {
   stack<int> s;
   s.push(1);
   s.push(2);
   s.push(3);
   for(int i = 1; i <= 3; i++) {
       cout << s.top() << “ “;
       s.pop();
   }
}
11. 

When a pop() operation is called on an empty queue, what is the condition called?

12. 

Which of the following data structures can be used to implement queues?

13. 

Which of the following data structures finds its use in recursion?

14. 

Which of the following data structures allow insertion and deletion from both ends?

15. 

What will be the output of the following code snippet?

void solve() {
   deque<int> dq;
   for(int i = 1; i <= 5; i++) {
       if(i % 2 == 0) {
           dq.push_back(i);
       }
       else {
           dq.push_front(i);
       }
   }
   for(auto x: dq) {
       cout << x << " ";
   }
   cout << endl;
}
16. 

Which of the following sorting algorithms provide the best time complexity in the worst-case scenario?

17. 

What is the maximum number of swaps that can be performed in the Selection Sort algorithm?

18. 

Which of the following is a Divide and Conquer algorithm?

19. 

What will be the best sorting algorithm, given that the array elements are small (<= 1e6)?

20. 

What will the output of the following code snippet be?

void solve() {
   vector<int> a = {1, 2, 3, 4, 5};
   sort(a.begin(), a.end(), [&](const int &x, const int &y) {
       return x % 2 < y % 2;
   });
   for(int x: a) {
       cout << x << " ";
   }
   cout << endl;
}
21. 

 What is the time complexity of the binary search algorithm?

22. 

What will be the output of the following code snippet?

void solve() {
   string s = "00000001111111";
   int l = 0, r = s.size() - 1, ans = -1;
   while(l <= r) {
       int mid = (l + r) / 2;
       if(s[mid] == '1') {
           ans = mid;
           r = mid - 1;
       }
       else {
           l = mid + 1;
       }
   }
   cout << ans << endl;
}
23. 

What will be the output of the following code snippet?

void solve() {
   int n = 24;
   int l = 0, r = 100, ans = n;
   while(l <= r) {
       int mid = (l + r) / 2;
       if(mid * mid <= n) {
           ans = mid;
           l = mid + 1;
       }
       else {
           r = mid - 1;
       }
   }
   cout << ans << endl;
}
24. 

What will be the output of the following code snippet?

int search(int l, int r, int target, vector<int> &a) {
   int mid = (l + r) / 2;
   if(a[mid] == target) {
       return mid;
   }
   else if(a[mid] < target) {
       return search(mid + 1, r, target, a);
   }
   else {
       return search(0, mid - 1, target, a);
   }
}
void solve() {
   vector<int> a = {1, 2, 3, 4, 5};
   cout << search(0, 4, 4, a) << endl;
}
25. 

What is the best case time complexity of the binary search algorithm?

26. 

What is the time complexity to insert an element to the front of a LinkedList(head pointer given)?

27. 

What is the time complexity to insert an element to the rear of a LinkedList(head pointer given)?

28. 

What will be the value of “sum” after the following code snippet terminates?

void solve(ListNode* root) {
   /*
   The LinkedList is defined as:
   root-> val = value of the node
   root-> next = address of next element from the node 
   The List is 1 -> 2 -> 3 -> 4 -> 5
   */
   int sum = 0;
   while(root -> next != NULL) {
       sum += root -> val;
       root = root -> next;
   }
   cout << sum << endl;
}
29. 

Which of the following can be done with LinkedList?

30. 

 What is the information, which a LinkedList’s Node must store?

31. 

What is the maximum number of children a node can have in an n-ary tree?

32. 

Worst case time complexity to access an element in a BST can be?

33. 

Which of the following represents the Postorder Traversal of a Binary Tree?

34. 

In what time complexity can we find the diameter of a binary tree optimally?

35. 

Which of the following statements is true about AVL Trees?

36. 

What does the following code snippet calculate (edges represent the adjacency list representation of a graph)?

void solve(vector<vector<int>> edges) {
   int count = 0;
   for(auto x: edges) {
       for(auto y: x) {
           count += 1;
       }
   }
   cout << count / 2 << endl;
}
37. 

In a graph of n nodes and n edges, how many cycles will be present?

38. 

A node in a tree, such that removing it splits the tree into forests, with size of each connected component being not greater than n / 2 is called?

39. 

What does the following code snippet do?

void dfs(int node, vector<vector<int>> &edges, vector<bool> &vis, vector<int> &dp) {
   vis[node] = true;
   for(auto x: edges[node]) {
       if(!vis[x]) {
           dp[x] = dp[node] + 1;
           dfs(x, edges, vis, dp);
       }
   }
}
40. 

Which of the following algorithms are used to find the shortest path from a source node to all other nodes in a weighted graph?

41. 

What is the best time complexity we can achieve to precompute all-pairs shortest paths in a weighted graph?

42. 

Which of the following are applications of Topological Sort of a graph?

43. 

Which of the following is known to be not an NP-Hard Problem?

44. 

Which of the following algorithms are used for string and pattern matching problems?

45. 

Consider we have a function, getLCA(), which returns us the Lowest Common Ancestor between 2 nodes of a tree. Using this getLCA() function, how can we calculate the distance between 2 nodes, given that distance from the root, to each node is calculated?

46. 

Which of the following algorithms are useful for processing queries on trees?

47. 

Consider the following code snippet:

void solve(vector<int> &a) {
   int queries;
   cin >> queries;
   while(queries--) {
       int type;
       cin >> type;
       if(type == 1) {
           int index, value;
           cin >> index >> value;
           update(a, index, value);
       }
       else {
           int l, r;
           cin >> l >> r;
           cout << getXOR(a, l, r) << endl;
       }
   }
}

The update() function updates the element at the given index in the array to some given value. The getXOR() function returns the XOR of the elements in the array a, in the range [l, r]. Which of the following data structures can perform the above tasks optimally?

48. 

Kruskal’s Algorithm for finding the Minimum Spanning Tree of a graph is a kind of a?

49. 

Maps in C++ are implemented using which of the following data structures?

50. 

What is the time complexity of the Sieve of Eratosthenes to check if a number is prime?

Your feedback is important to help us improve.