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.

351.

What is the best case complexity of selection sort?(a) O(nlogn)(b) O(logn)(c) O(n)(d) O(n^2)This question was addressed to me in an interview for job.Query is from Selection Sort topic in chapter Sorting of Data Structures & Algorithms II

Answer»

The CORRECT choice is (d) O(n^2)

The BEST I can explain: The best, average and worst case complexities of selection SORT is O(n^2).

(n-1) + (n-2) + (n-3) + …. + 1 = (n(n-1))/2 ~ (n^2)/2.

352.

The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are __________(a) 5 and 4(b) 1 and 4(c) 0 and 4(d) 4 and 1The question was posed to me in a job interview.Query is from Selection Sort in chapter Sorting of Data Structures & Algorithms II

Answer»

Correct option is (d) 4 and 1

The best I can EXPLAIN: SELECTION sort is insensitive to input, HENCE 4(n-1) ITERATIONS. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.

353.

The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are __________(a) 5 and 4(b) 4 and 5(c) 2 and 4(d) 2 and 5This question was posed to me at a job interview.Origin of the question is Selection Sort in chapter Sorting of Data Structures & Algorithms II

Answer»

Right option is (a) 5 and 4

Best EXPLANATION: Since the INPUT array is not SORTED, bubble sort takes 5 iterations and selection sort takes 4(n-1) iterations.

354.

What is the disadvantage of selection sort?(a) It requires auxiliary memory(b) It is not scalable(c) It can be used for small keys(d) It takes linear time to sort the elementsThe question was posed to me in an interview for internship.The query is from Selection Sort topic in section Sorting of Data Structures & Algorithms II

Answer»

The correct answer is (b) It is not scalable

The explanation is: As the input SIZE increases, the PERFORMANCE of selection sort DECREASES.

355.

What is the average case complexity of selection sort?(a) O(nlogn)(b) O(logn)(c) O(n)(d) O(n^2)This question was addressed to me during an online interview.Origin of the question is Selection Sort topic in section Sorting of Data Structures & Algorithms II

Answer» CORRECT choice is (d) O(n^2)

The EXPLANATION is: In the average CASE, even if the input is partially sorted, selection sort behaves as if the ENTIRE array is not sorted. Selection sort is insensitive to input.
356.

Which of the following is not an exchange sort?(a) Bubble Sort(b) Quick Sort(c) Partition-exchange Sort(d) Insertion SortThe question was asked in quiz.Question is from Insertion sort topic in chapter Sorting of Data Structures & Algorithms II

Answer»

Right answer is (d) Insertion Sort

Easiest explanation - In Exchange sorts, we compare each element of an ARRAY and SWAP those elements that are not in their PROPER position. BUBBLE Sort and Quick Sort are exchange sorts. Quick Sort is ALSO called as Partition-exchange Sort. Insertion sort is not an exchange sort.

357.

In insertion sort, the average number of comparisons required to place the 7^th element into its correct position is ____(a) 9(b) 4(c) 7(d) 14This question was addressed to me in an online quiz.This key question is from Insertion sort in portion Sorting of Data Structures & Algorithms II

Answer»

Right OPTION is (b) 4

Best explanation: On average (k + 1) / 2 comparisons are required to place the k^th ELEMENT into its correct position. THEREFORE, average number of comparisons required for 7th element = (7 + 1)/2 = 4.

358.

Statement 1: In insertion sort, after m passes through the array, the first m elements are in sorted order.Statement 2: And these elements are the m smallest elements in the array.(a) Both the statements are true(b) Statement 1 is true but statement 2 is false(c) Statement 1 is false but statement 2 is true(d) Both the statements are falseI got this question during an online interview.I need to ask this question from Insertion sort in section Sorting of Data Structures & Algorithms II

Answer»

Right option is (b) Statement 1 is TRUE but statement 2 is false

The best I can explain: In insertion SORT, after m passes through the array, the first m ELEMENTS are in sorted order but they are whatever the first m elements were in the UNSORTED array.

359.

Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of insertions done while running insertion sort on the array?(a) 7 9 4 2 14 7 9 2 12 4 7 9 11 2 4 7 9(b) 9 7 4 1 29 7 1 2 49 1 2 4 71 2 4 7 9(c) 7 4 2 1 94 2 1 9 72 1 9 7 41 9 7 4 2(d) 7 9 4 2 12 4 7 9 14 7 9 2 11 2 4 7 9I have been asked this question at a job interview.I would like to ask this question from Insertion sort in section Sorting of Data Structures & Algorithms II

Answer»

Right option is (a) 7 9 4 2 1

4 7 9 2 1

2 4 7 9 1

1 2 4 7 9

Easiest explanation - The steps performed while RUNNING insertion sort on given array are:

INITIAL : 9 7 4 2 1 key = 7

7 9 4 2 1 key = 4

4 7 9 2 1 key = 2

2 4 7 9 1 key = 1

1 2 4 7 9

In each step, the key is the element that is compared with the elements PRESENT at the left side to it.

360.

Which of the following is good for sorting arrays having less than 100 elements?(a) Quick Sort(b) Selection Sort(c) Merge Sort(d) Insertion SortI had been asked this question in class test.The origin of the question is Insertion sort in portion Sorting of Data Structures & Algorithms II

Answer» CORRECT choice is (d) INSERTION SORT

The best explanation: The insertion sort is good for sorting small ARRAYS. It sorts smaller arrays faster than any other sorting ALGORITHM.
361.

Insertion sort is an example of an incremental algorithm.(a) True(b) FalseThe question was asked during an interview.My question is based upon Insertion sort in section Sorting of Data Structures & Algorithms II

Answer»

The correct answer is (a) True

Explanation: In the incremental algorithms, the complicated structure on n ITEMS is built by first BUILDING it on n − 1 items. And then we make the necessary changes to fix things in adding the last item. Insertion sort builds the sorted sequence one ELEMENT at a time. Therefore, it is an example of an incremental ALGORITHM.

362.

The worst case time complexity of insertion sort is O(n^2). What will be the worst case time complexity of insertion sort if the correct position for inserting element is calculated using binary search?(a) O(nlogn)(b) O(n^2)(c) O(n)(d) O(logn)The question was asked by my school principal while I was bunking the class.Question is taken from Insertion sort topic in portion Sorting of Data Structures & Algorithms II

Answer» CORRECT answer is (b) O(n^2)

For explanation: The use of binary search reduces the TIME of finding the correct position from O(n) to O(LOGN). But the worst case of insertion sort REMAINS O(n^2) because of the series of swapping operations required for each insertion.
363.

Which of the following sorting algorithm is best suited if the elements are already sorted?(a) Heap Sort(b) Quick Sort(c) Insertion Sort(d) Merge SortI got this question during an online interview.I'd like to ask this question from Insertion sort in chapter Sorting of Data Structures & Algorithms II

Answer»

Right option is (c) INSERTION Sort

The best I can explain: The best case running time of the insertion sort is O(n). The best case occurs when the input ARRAY is ALREADY sorted. As the elements are already sorted, only one comparison is made on each PASS, so that the time required is O(n).

364.

Which of the following is correct with regard to insertion sort?(a) insertion sort is stable and it sorts In-place(b) insertion sort is unstable and it sorts In-place(c) insertion sort is stable and it does not sort In-place(d) insertion sort is unstable and it does not sort In-placeI had been asked this question in an interview for internship.My question is taken from Insertion sort in division Sorting of Data Structures & Algorithms II

Answer»

The CORRECT option is (a) insertion sort is stable and it sorts In-place

Easiest EXPLANATION - During insertion sort, the relative ORDER of elements is not changed. Therefore, it is a stable SORTING algorithm. And insertion sort requires only O(1) of ADDITIONAL memory space. Therefore, it sorts In-place.

365.

Which of the following examples represent the worst case input for an insertion sort?(a) array in sorted order(b) array sorted in reverse order(c) normal unsorted array(d) large arrayThe question was posed to me in my homework.I'd like to ask this question from Insertion sort in chapter Sorting of Data Structures & Algorithms II

Answer»

The correct option is (B) ARRAY SORTED in reverse order

The explanation is: The worst case INPUT for an insertion sort ALGORITHM will be an array sorted in reverse order and its running time is quadratic.

366.

For the best case input, the running time of an insertion sort algorithm is?(a) Linear(b) Binary(c) Quadratic(d) Depends on the inputThis question was posed to me by my college director while I was bunking the class.I'd like to ask this question from Insertion sort in chapter Sorting of Data Structures & Algorithms II

Answer»

The correct CHOICE is (a) Linear

To explain: The best case INPUT for an insertion sort ALGORITHM runs in linear TIME and is given by O(N).

367.

Which of the following sorting algorithms is the fastest for sorting small arrays?(a) Quick sort(b) Insertion sort(c) Shell sort(d) Heap sortThis question was addressed to me during an interview for a job.This question is from Insertion sort in section Sorting of Data Structures & Algorithms II

Answer»

The correct ANSWER is (B) Insertion sort

Easy EXPLANATION - For sorting small ARRAYS, insertion sort runs even FASTER than quick sort. But, it is impractical to sort large arrays.

368.

Which of the following options contain the correct feature of an insertion sort algorithm?(a) anti-adaptive(b) dependable(c) stable, not in-place(d) stable, adaptiveThe question was asked in examination.The question is from Insertion sort topic in section Sorting of Data Structures & Algorithms II

Answer»

Correct answer is (d) STABLE, adaptive

Best explanation: An insertion SORT is stable, adaptive, in-place and incremental in nature.

369.

Binary search can be used in an insertion sort algorithm to reduce the number of comparisons.(a) True(b) FalseThe question was asked in examination.This intriguing question comes from Insertion sort topic in chapter Sorting of Data Structures & Algorithms II

Answer» RIGHT ANSWER is (a) True

Easy explanation - Binary search can be used in an INSERTION SORT algorithm to reduce the NUMBER of comparisons. This is called a Binary insertion sort.
370.

In C, what are the basic loops required to perform an insertion sort?(a) do- while(b) if else(c) for and while(d) for and ifThis question was posed to me by my school teacher while I was bunking the class.The doubt is from Insertion sort in division Sorting of Data Structures & Algorithms II

Answer»

Right ANSWER is (c) for and while

For explanation: To PERFORM an insertion SORT, we use two basic loops- an OUTER for LOOP and an inner while loop.

371.

Which of the following real time examples is based on insertion sort?(a) arranging a pack of playing cards(b) database scenarios and distributes scenarios(c) arranging books on a library shelf(d) real-time systemsThe question was asked by my college director while I was bunking the class.This interesting question is from Insertion sort topic in section Sorting of Data Structures & Algorithms II

Answer»

The correct option is (a) arranging a pack of playing cards

Easiest EXPLANATION - Arranging a pack of cards mimics an insertion SORT. Database SCENARIO is an EXAMPLE for MERGE sort, arranging books is a stack and real-time systems uses quick sort.

372.

What is the running time of an insertion sort algorithm if the input is pre-sorted?(a) O(N^2)(b) O(N log N)(c) O(N)(d) O(M log N)This question was addressed to me in quiz.This interesting question is from Insertion sort in portion Sorting of Data Structures & Algorithms II

Answer»

The correct choice is (c) O(N)

Easy explanation - If the INPUT is pre-sorted, the running time is O(N), because the test in the inner for loop ALWAYS fails immediately and the ALGORITHM will RUN QUICKLY.

373.

What is the average number of inversions in an array of N distinct numbers?(a) N(N-1)/4(b) N(N+1)/2(c) N(N-1)/2(d) N(N-1)/3I got this question in my homework.I want to ask this question from Insertion sort topic in chapter Sorting of Data Structures & Algorithms II

Answer»

Right answer is (a) N(N-1)/4

Easiest explanation - The total number of PAIRS in a list L is N(N-1)/2. Thus, an average list has half this amount, or N(N-1)/4 inversions.

374.

Any algorithm that sorts by exchanging adjacent elements require O(N^2) on average.(a) True(b) FalseThis question was posed to me during an interview.My doubt stems from Insertion sort topic in portion Sorting of Data Structures & Algorithms II

Answer»

Correct ANSWER is (a) True

To explain: Each swap removes only ONE inversion, so O(N^2) swaps are REQUIRED.

375.

What is the average case running time of an insertion sort algorithm?(a) O(N)(b) O(N log N)(c) O(log N)(d) O(N^2)I had been asked this question at a job interview.This intriguing question comes from Insertion sort in chapter Sorting of Data Structures & Algorithms II

Answer»

Correct choice is (d) O(N^2)

The best I can explain: The AVERAGE CASE analysis of a tight bound ALGORITHM is MATHEMATICALLY achieved to be O(N^2).

376.

Which of the following algorithm implementations is similar to that of an insertion sort?(a) Binary heap(b) Quick sort(c) Merge sort(d) Radix sortI had been asked this question in an online interview.Asked question is from Insertion sort topic in chapter Sorting of Data Structures & Algorithms II

Answer»

Correct option is (a) Binary heap

The EXPLANATION is: INSERTION sort is SIMILAR to that of a binary heap ALGORITHM because of the USE of temporary variable to swap.

377.

How many passes does an insertion sort algorithm consist of?(a) N(b) N-1(c) N+1(d) N^2This question was addressed to me during an online interview.The origin of the question is Insertion sort topic in portion Sorting of Data Structures & Algorithms II

Answer» CORRECT option is (b) N-1

Best explanation: An insertion ALGORITHM consists of N-1 PASSES when an ARRAY of N elements is given.