Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What will be the time complexity of the code to find a minimum element from an array of size n and uses square root decomposition(exclude pre processing time)?(a) O(√n)(b) O(n)(c) O(1)(d) O(n^2)The question was posed to me during an interview for a job.My question comes from Miscellaneous in division Miscellaneous of Data Structures & Algorithms II

Answer»

Correct OPTION is (a) O(√n)

Best EXPLANATION: For finding the minimum element in a given array of size n using square root decomposition we FIRST divide the array into √n CHUNKS and calculate the result for them individually. So for a given query, the result of middle blocks has to be calculated along with the extreme elements. This takes O(√n) time in the worst case.

2.

Mo’s algorithm can only be used for problems where the query can be calculated from the result of the previous query.(a) true(b) falseI had been asked this question in an interview.My doubt is from Miscellaneous in chapter Miscellaneous of Data Structures & Algorithms II

Answer» RIGHT answer is (a) true

The explanation is: Mo’s ALGORITHM uses the result of the previous query in ORDER to compute the result of the given query. It cannot be implemented where such a SCENARIO is not POSSIBLE.
3.

What will be the worst case time complexity of code to find sum in given query range (l,r) in an array of size n with q number of such queries when we apply MO’s algorithm?(a) O(n*q)(b) O(n)(c) O((q+n)√n)(d) O(q*√n)The question was asked in an interview for internship.This question is from Miscellaneous in portion Miscellaneous of Data Structures & Algorithms II

Answer»

Correct option is (C) O((q+N)√n)

For explanation: MO’s ALGORITHM requires O(q*√n) + O(n*√n) time for processing all the QUERIES. It is better than the naive solution where O(n*q) time is required.

4.

What will be the worst case time complexity of code to find sum in given query range (l,r) in an array of size n with q number of such queries?(a) O(n)(b) O(q)(c) O(n*q)(d) O(n+q)I have been asked this question in an online quiz.The above asked question is from Miscellaneous topic in portion Miscellaneous of Data Structures & Algorithms II

Answer»

Right answer is (c) O(n*q)

Easy EXPLANATION - For finding the RESULT of one query the WORST case time COMPLEXITY will be n. So for q queries the time complexity will be O(q*n). This can be reduced by using square root optimization.

5.

Square root decomposition technique is only applicable when the number of indices in an array is a perfect square.(a) true(b) falseThe question was asked in class test.I want to ask this question from Miscellaneous in division Miscellaneous of Data Structures & Algorithms II

Answer» RIGHT choice is (b) false

For EXPLANATION: Square root decomposition TECHNIQUE can be applied to an ARRAY with any number of indices. It does not REQUIRE this number to be a perfect square.
6.

What will be the time complexity of update query operation in an array of size n when we use square root optimization?(a) O(√n)(b) O(n)(c) O(1)(d) O(n^2)The question was posed to me by my school teacher while I was bunking the class.I need to ask this question from Miscellaneous in division Miscellaneous of Data Structures & Algorithms II

Answer»

The correct option is (C) O(1)

The best I can explain: The time complexity of QUERY operation remains the same in both square ROOT optimized code and non optimized code. We simply find the chunk in which the update requires to be PERFORMED and then add the new updated value at the desired index.

7.

Total how many iterations are required to find the sum of elements in a given range of (l,r) in an array of size n when we use square root optimization?(a) √n(b) 2*√n(c) 3*√n(d) n*√nThis question was posed to me in an interview for internship.This key question is from Miscellaneous in portion Miscellaneous of Data Structures & Algorithms II

Answer»

Right choice is (c) 3*√n

Easiest explanation - After calculating the sum of each chunk individually we require to iterate only 3*√n times to calculate the sum in the WORST case. It is because two of the √n FACTORS consider the worst case time complexity of summing ELEMENTS in the FIRST and last block. Whereas the third √n considers the factor of summing the √n chunks.

8.

What will be the worst case time complexity of finding the sum of elements in a given range of (l,r) in an array of size n when we use square root optimization?(a) O(n)(b) O(l+r)(c) O(√n)(d) O(r-l)This question was posed to me during an interview.I need to ask this question from Miscellaneous in section Miscellaneous of Data Structures & Algorithms II

Answer»

Correct choice is (C) O(√n)

The best explanation: When we use SQUARE root optimization we decompose the given ARRAY into √n chunks each of size √n. So after calculating the sum of each chunk individually, we require to iterate only 3*√n times to calculate the sum in the worst CASE.

9.

What will be the worst case time complexity of finding the sum of elements in a given range of (l,r) in an array of size n?(a) O(n)(b) O(l+r)(c) O(l-r)(d) O(r-l)This question was addressed to me during a job interview.I need to ask this question from Miscellaneous in division Miscellaneous of Data Structures & Algorithms II

Answer»

Correct answer is (a) O(n)

For explanation: For a GIVEN array of size n we have to TRAVERSE all n elements in the WORST CASE. In such a case l=0, r=n-1 so the time complexity will be O(n).

10.

By what factor time complexity is reduced when we apply square root decomposition to a code?(a) n(b) √n(c) n^2(d) n^-1/2This question was addressed to me during an online exam.Origin of the question is Miscellaneous topic in chapter Miscellaneous of Data Structures & Algorithms II

Answer» CORRECT answer is (b) √n

The best EXPLANATION: In square root DECOMPOSITION a given array is DECOMPOSED into SMALL parts each of size √n. This reduces the time complexity of the code by a factor of √n.
11.

What is the purpose of using square root decomposition?(a) to reduce the time complexity of a code(b) to increase the space complexity of a code(c) to reduce the space complexity of a code(d) to reduce the space and time complexity of a codeThe question was posed to me in an interview for job.My question is from Miscellaneous in division Miscellaneous of Data Structures & Algorithms II

Answer»

The CORRECT ANSWER is (a) to reduce the time complexity of a code

Explanation: Square decomposition is mainly used in competitive programming to optimize code. It REDUCES the time complexity by a factor of √n.

12.

Co-ordinate compression can only be applied in a co-ordinate system problem.(a) true(b) falseThe question was posed to me in an international level competition.Question is taken from Miscellaneous topic in section Miscellaneous of Data Structures & Algorithms II

Answer»

The CORRECT choice is (B) false

Easy explanation - Co-ordinate compression technique can be applied where such optimization is suitable. It does not require to co-ordinate SYSTEM PROBLEM only.

13.

Co-ordinate compression reduces the number of squares in a graph.(a) true(b) falseThe question was posed to me in semester exam.The origin of the question is Miscellaneous topic in chapter Miscellaneous of Data Structures & Algorithms II

Answer»

Right choice is (a) true

The EXPLANATION is: The idea behind co-ordinate COMPRESSION is to reduce the NUMBER of squares in a graph by converting them into rectangles of viable size. This REDUCES the TIME complexity of traversal.

14.

What is the need for co-ordinate compression?(a) for improving time complexity(b) for improving space complexity(c) for improving both time and space complexity(d) for making code simplerThe question was asked by my college director while I was bunking the class.This key question is from Miscellaneous in chapter Miscellaneous of Data Structures & Algorithms II

Answer»

The correct choice is (c) for improving both time and space complexity

Best explanation: Co-ordinate COMPRESSION is the PROCESS of reassigning co-ordinates in order to remove gaps. This HELPS in improving both time and space complexity of a SOLUTION.

15.

What is co-ordinate compression?(a) process of reassigning co-ordinates to remove gaps(b) inserting gaps in a co-ordinate system(c) removing redundant co-ordinates(d) adding extra gapsI have been asked this question in semester exam.This interesting question is from Miscellaneous in portion Miscellaneous of Data Structures & Algorithms II

Answer»

The CORRECT option is (a) process of REASSIGNING co-ordinates to REMOVE GAPS

Explanation: Co-ordinate compression is the process of reassigning co-ordinates in order to remove gaps. This HELPS in improving efficiency of a solution.

16.

What is the best case time complexity of quickselect?(a) O(n log n)(b) O(n^2)(c) O(n)(d) O(log n)I got this question in final exam.This is a very interesting question from Miscellaneous in portion Miscellaneous of Data Structures & Algorithms II

Answer»

The correct OPTION is (c) O(n)

Easiest explanation - Best case TIME COMPLEXITY of QUICKSELECT is O(n). It is observed in the case where good pivots are chosen CONSISTENTLY then the array size decreases exponentially and thus the required element is found in linear time.

17.

Quickselect is an in-place algorithm?(a) true(b) falseI had been asked this question in an interview.This interesting question is from Miscellaneous in section Miscellaneous of Data Structures & Algorithms II

Answer» RIGHT option is (a) true

To EXPLAIN: QUICKSELECT’s AUXILIARY space requirement is O(1). So quickselect qualifies as an in-place ALGORITHM.
18.

What is the auxiliary space requirement of the quickselect algorithm?(a) O(n^2)(b) O(n)(c) O(n log n)(d) O(1)This question was addressed to me in examination.I want to ask this question from Miscellaneous topic in division Miscellaneous of Data Structures & Algorithms II

Answer»

Correct choice is (d) O(1)

Easy explanation - Quickselect algorithm requires no extra SPACE in order to calculate the DESIRED result. It PERFORMS manipulations in the given ARRAY itself so its AUXILIARY space requirement will be O(1).

19.

What will be the output if quickselect algorithm is applied to the array arr={1,5,4,3,7} with k given as 4?(a) 1(b) 3(c) 4(d) 5I had been asked this question in an international level competition.This intriguing question comes from Miscellaneous in section Miscellaneous of Data Structures & Algorithms II

Answer»

Correct choice is (d) 5

The EXPLANATION is: QUICKSELECT algorithm finds the kth smallest element from the GIVEN list. So as here the given value of k is 4 so we need to FIND the fourth smallest element which is 5 in the given ARRAY.

20.

Quickselect is an example of ___________(a) sorting algorithm(b) selection algorithm(c) greedy algorithm(d) searching algorithmThe question was asked during a job interview.I need to ask this question from Miscellaneous topic in chapter Miscellaneous of Data Structures & Algorithms II

Answer» RIGHT choice is (B) selection algorithm

Explanation: QUICKSELECT is an EXAMPLE of a selection algorithm. It finds the kth smallest element from the given list.
21.

Which of the following is an alternative name of the quickselect algorithm?(a) quick sort(b) hoare’s selection algorithm(c) tony’s selection algorithm(d) kruskal’s algorithmI have been asked this question during an interview for a job.The query is from Miscellaneous in section Miscellaneous of Data Structures & Algorithms II

Answer»

The correct option is (B) hoare’s selection algorithm

To EXPLAIN: Quick SELECT is a selection algorithm. It was developed by Tony Hoare, THUS it is ALSO known as Hoare’s selection algorithm.

22.

When the topological sort of a graph is unique?(a) When there exists a hamiltonian path in the graph(b) In the presence of multiple nodes with indegree 0(c) In the presence of single node with indegree 0(d) In the presence of single node with outdegree 0I have been asked this question during a job interview.My question comes from Topological Sort topic in portion Miscellaneous of Data Structures & Algorithms II

Answer»

Right answer is (a) When there exists a hamiltonian PATH in the graph

Easiest explanation - A hamiltonian path exists in a Directed ACYCLIC Graph when all pairs of consecutive vertices are in sorted ORDER and are connected by edges. In such a case, there exists a unique TOPOLOGICAL sorting order.

23.

A man wants to go different places in the world. He has listed them down all. But there are some places where he wants to visit before some other places. What application of graph can he use to determine that?(a) Depth First Search(b) Breadth First Search(c) Topological Sorting(d) Dijkstra’s Shortest path algorithmI had been asked this question during a job interview.This interesting question is from Topological Sort topic in chapter Miscellaneous of Data Structures & Algorithms II

Answer»

Right choice is (c) TOPOLOGICAL Sorting

Best EXPLANATION: As the definition of topological sorting suggests, it is the way to do tasks in PRESCRIBED ORDER. So, if he does topological sorting, it will be easy for him to recognize what should be the order to visit different PLACES.

24.

Topological sort is equivalent to which of the traversals in trees?(a) Pre-order traversal(b) Post-order traversal(c) In-order traversal(d) Level-order traversalThis question was posed to me by my college professor while I was bunking the class.Asked question is from Topological Sort in division Miscellaneous of Data Structures & Algorithms II

Answer»

Correct answer is (a) Pre-order traversal

The best I can EXPLAIN: In pre-order traversal of TREES, we process the root FIRST and then CHILD from LEFT to right.

25.

Topological sort can be implemented by?(a) Using Depth First Search(b) Using Breadth First Search(c) Using Depth and Breadth First Search(d) Using level ordered searchI got this question in semester exam.I would like to ask this question from Topological Sort in division Miscellaneous of Data Structures & Algorithms II

Answer»

Right option is (c) Using Depth and Breadth First Search

For explanation: We can implement topological sort by both BFS and DFS. In BFS, we use queue as data structure and in DFS, we use LINKED LIST (if recursive) or Stack (if not recursive) as data structure.

26.

Topological sort of a Directed Acyclic graph is?(a) Always unique(b) Always Not unique(c) Sometimes unique and sometimes not unique(d) Always unique if graph has even number of verticesThe question was posed to me during an interview for a job.I'm obligated to ask this question of Topological Sort in chapter Miscellaneous of Data Structures & Algorithms II

Answer» CORRECT choice is (C) Sometimes unique and sometimes not unique

To explain: The topological SORT of a graph can be unique if we assume the graph as a single linked list and we can have multiple topological sort ORDER if we consider a graph as a complete binary tree.
27.

Which of the following is not an application of topological sorting?(a) Finding prerequisite of a task(b) Finding Deadlock in an Operating System(c) Finding Cycle in a graph(d) Ordered StatisticsI have been asked this question during an online interview.I'm obligated to ask this question of Topological Sort topic in section Miscellaneous of Data Structures & Algorithms II

Answer»

Right choice is (d) Ordered Statistics

Best explanation: Topological sort TELLS what task should be DONE before a task can be STARTED. It also detects cycle in the GRAPH which is why it is used in the Operating System to find the deadlock. Ordered statistics is an APPLICATION of Heap sort.

28.

In most of the cases, topological sort starts from a node which has __________(a) Maximum Degree(b) Minimum Degree(c) Any degree(d) Zero DegreeI have been asked this question during an online exam.Enquiry is from Topological Sort in portion Miscellaneous of Data Structures & Algorithms II

Answer»

The CORRECT OPTION is (d) Zero DEGREE

Easiest explanation - Topological sort starts with a node which has zero degree. If MULTIPLE such nodes EXISTS then it can start with any node.

29.

Most Efficient Time Complexity of Topological Sorting is? (V – number of vertices, E – number of edges)(a) O(V + E)(b) O(V)(c) O(E)(d) O(V*E)The question was asked in a job interview.This intriguing question comes from Topological Sort topic in division Miscellaneous of Data Structures & Algorithms II

Answer»

The correct choice is (a) O(V + E)

Explanation: The TOPOLOGICAL sort ALGORITHM has complexity same as Depth FIRST SEARCH. So, DFS has a complexity O(V+E).

30.

Topological sort can be applied to which of the following graphs?(a) Undirected Cyclic Graphs(b) Directed Cyclic Graphs(c) Undirected Acyclic Graphs(d) Directed Acyclic GraphsI have been asked this question during a job interview.The doubt is from Topological Sort topic in section Miscellaneous of Data Structures & Algorithms II

Answer»

Correct answer is (d) DIRECTED ACYCLIC Graphs

Easiest explanation - Every Directed Acyclic Graph has one or more topological ordering WHEREAS Cyclic and Undirected graphs can’t be ORDERED TOPOLOGICALLY.