True or False: Stack

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

True Or False

1) Algorithm should have finite number of steps. true


2) Running the program many times is sufficient to prove the correctness of program.
false
Q 2:
a) Which Data Structure is used to perform Recursion?
A : Array
B : queue
C : stack
D : linked list

b) Algorithm can be represented as


A : Pseudocode
B : Flowchart
C : None of the above
D : both A and B

c) Which of the following algorithms has worst time complexity?


A : insertion sort
B : binary search
C : linear search
D : merge sort

d) Which data structure is used for implementing a FIFO branch and bound strategy?
A : Stack
B : Queue
C : Array
D : Linked List

e) A ___ is a compact, informal, and environment-independent description of a computer


programming algorithm.
a. Stack
b. Queue
c. Psuedocode
d. Non-linear data structure

f) ___ of an algorithm is the amount of time required for it to execute.


a. Time complexity
b. Space complexity
c. Compiling time
d. Best case

g) ___ is the maximum amount of time an algorithm takes to execute a specific set of inputs.
a. Running time
b. Average case time complexity
c. Worst case time complexity
d. Best case time complexity

Q 3:
1. Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero
integers m and n. write an Euclid’s algorithm (in steps)
2. Design an algorithm to find all the common elements in two sorted lists of numbers. For
example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the
maximum number of comparisons your algorithm makes if the lengths of the two given
lists are m and n respectively?
(answ) :
Initialize the list of common elements to empty.
Starting with the first elements of the lists given, repeat the following until one of the lists
becomes empty.
Compare the current elements of the two lists: if they are equal, add this element to the list of
common elements and move to the next elements of both lists (if any); otherwise, move to the
element following the smaller of the two involved in the comparison.
The maximum number of comparisons, which is made by this algorithm on some lists with no
common elements such as the first m positive odd numbers and the first n positive even numbers,
is equal to m + n – 1

Q 4:
What is time complexity of fun()?

int fun(int n)
{
int count = 0;
for (int i = n; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count += 1;
return count;
}

(A) O(n^2)
(B) O(nLogn)
(C) O(n)
(D) O(nLognLogn)
Explanation: For a input integer n, the innermost statement of fun() is executed
following times.
n + n/2 + n/4 + … 1
So time complexity T(n) can be written as
T(n) = O(n + n/2 + n/4 + … 1) = O(n)
The value of count is also n + n/2 + n/4 + .. + 1

What is the time complexity of fun ( )?

int fun(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j > 0; j--)
count = count + 1;
return count;
}

Assuming the inner loop is (int j = i; j > 0; j--).

When i = 0, it executes 0 times.


When i = 1, it executes 1 time.
When i = 2, it executes 2 time.
When i = 3, it executes 3 times and so on till when i = n-1, j will also be executed n -1
times.
The number of basic operations performed will be 1+2+3+⋯+n−11+2+3+⋯+n−1 which is
known to be O(n2).

Question :
1) Two main measures for the efficiency of an algorithm are ……………., ……………….
2) List the most important problem types in design and analysis of algorithm
1) …………………………………..
2) ……………………………………
3) …………………………………...
4) ……………………………………
5) ……………………………………
6) ……………………………………

1) sorting
2) searching
3) string processing
4) graph problems
5) combinatorial problems
6) geometric problems
7) numerical problems

3) The running time T(n) of a program implementing this algorithm on that computer by
the formula
T (n) ≈ cop C(n).
Where cop is ............ execution time for basic operation.......................
and C(n) is .................. Number of times basic operation is executed ........................

4) (Chose the correct answer(s))


1- Algorithms can be specified in a natural language or pseudocode; they can also be
implemented as computer programs.
a. True
b. False
2- A ___ is a compact, informal, and environment-independent description of a computer
programming algorithm.
a. Stack c. Pseudocode
b. Queue d. Non-linear data structure

3- The stack is also known as?


a. Last in first out b. First in last out
c. First in first out d. none of these

4- Suppose we want to determine the efficiency of the algorithm, then how we can measure
the space factor.
a. To count the maximum memory required by the algorithm
b. To count the minimum memory required by the algorithm
c. To count the average memory required by the algorithm
d. To count the maximum disk space needed by the algorithm

Question 2: [12 Marks]

1- Consider the following algorithm.

ALGORITHM Mystery(n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S←S+i∗i
return S

a) What does this algorithm compute?


b) What is its basic operation?
c) How many times is the basic operation executed?
2- Find the minimum number of operations required for the following matrix chain
multiplication using dynamic programming:

A(10 × 30) * B (30 × 5) * C (5 × 60)

However, the order in which the product is parenthesized affects the number of simple arithmetic
operations needed to compute the product. For example, if A is a 10 × 30 matrix, B is a 30 × 5
matrix, and C is a 5 × 60 matrix, then computing (AB)C needs (10×30×5) + (10×5×60) = 1500 +
3000 = 4500 operations while computing A(BC) needs (30×5×60) + (10×30×60) = 9000 + 18000
= 27000 operations. Clearly, the first method is more efficient.

3- Identify and count the basic operation(s) of fun ( )?


int fun(int n)
{
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i; j > 0; j--)
count = count + 1;
return count;
}

4- Fined the Input size and basic operation of the following examples
Problem Input size measure Basic operation

Find x in an array

Multiplication of two matrices

Sort an array of numbers

Traverse a tree

Decrease-and-Conquer
Insertion Sort

1. How many passes does an insertion sort algorithm consist of?


a) N
b) N-1
c) N+1
d) N2

Answer: b
Explanation: An insertion algorithm consists of N-1 passes when an array of N elements is
given.

2. 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 sort
Answer: a
Explanation: Insertion sort is similar to that of a binary heap algorithm because of the use of
temporary variable to swap.

3. 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(N2)

Answer: d
Explanation: The average case analysis of a tight bound algorithm is mathematically achieved
to be O(N2).

4. Any algorithm that sorts by exchanging adjacent elements require O(N2) on average.
a) True
b) False

Answer: a
Explanation: Each swap removes only one inversion, so O(N2) swaps are required.

5. 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)/3

Answer: a
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.

6. What is the running time of an insertion sort algorithm if the input is pre-sorted?
a) O(N2)
b) O(N log N)
c) O(N)
d) O(M log N)

Answer: c
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.

7. What will be the number of passes to sort the elements using insertion sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1

Answer: b
Explanation: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.
8. For the following question, how will the array elements look like after second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21

Answer: d
Explanation: After swapping elements in the second pass, the array will look like, 8, 34, 64, 51,
32, 21.

9. 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 systems

Answer: a
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.

10. 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 if

Answer: c
Explanation: To perform an insertion sort, we use two basic loops- an outer for loop and an
inner while loop.

11. Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons.
a) True
b) False

Answer: a
Explanation: Binary search can be used in an insertion sort algorithm to reduce the number of
comparisons. This is called a Binary insertion sort.

12. 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, adaptive
Answer: d
Explanation: An insertion sort is stable, adaptive, in-place and incremental in nature.

13. Which of the following sorting algorithms is the fastest for sorting small arrays?
a) Quick sort
b) Insertion sort
c) Shell sort
d) Heap sort

Answer: b
Explanation: For sorting small arrays, insertion sort runs even faster than quick sort. But, it is
impractical to sort large arrays.

14. For the best case input, the running time of an insertion sort algorithm is?
a) Linear
b) Binary
c) Quadratic
d) Depends on the input

Answer: a
Explanation: The best case input for an insertion sort algorithm runs in linear time and is given
by O(N).

15. 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 array

Answer: bExplanation: The worst case input for an insertion sort algorithm will be an array
sorted in reverse order and its running time is quadratic.

-----------------------------------------------------------

Questions and Answers

1. Consider the following lists of partially sorted numbers. The double bars (||) represent the
sort of marker. How many comparisons and swaps are needed to sort the next number.

[1 3 4 8 9 || 5 2]

A. 2 comparisons, 3 swaps

B. 3 comparisons, 2 swaps

C. 4 comparisons, 3 swaps

D. 3 comparisons, 4 swaps

2. Consider the following lists of partially sorted numbers. The double bars represent the sort
marker. How many comparisons and swaps are needed to sort the next number. [1 3 4 5 8 9 ||
2]

A. 5 comparisons, 4 swaps

B. 4 comparisons, 5 swaps

C. 6 comparisons, 5 swaps

D. 5 comparisons, 6 swaps
3. If all the elements in an input array is equal for example {1,1,1,1,1,1}, What would be the
running time of the Insertion Algorithm?

A. O(2N)

B. O(n^2)

C. O(n)

D. None of the above

4. What operation does the Insertion Sort use to move numbers from the unsorted section to
the sorted section of the list?

A. Finding the minimum value

B. Swapping

C. Finding out an pivot value

D. None of the above

5. What are the correct intermediate steps of the following data set when it is being sorted
with the Insertion sort? 15,20,10,18

A. 15,20,10,18 -- 10,15,20,18 -- 10,15,18,20 -- 10,15,18,20

B. 15,18,10,20 -- 10,18,15,20 -- 10,15,18,20 -- 10,15,18,20

C. 15,10,20,18 -- 15,10,18,20 -- 10,15,18,20

D. 10, 20,15,18 -- 10,15,20,18 -- 10,15,18,20

6. What will be the number of passes to sort the elements using insertion sort? 14, 12,16, 6, 3,
10

A. 7

B. 8

C. 1

D. 5

7. Which of the following options contain the correct feature of an insertion sort algorithm?

A. Dependable

B. Stable, adaptive

C. Anti-stable

D. None of the above


8. State true or false. Binary search can be used in an insertion sort algorithm to reduce the
number of comparisons.

A. True

B. False

9.Which of these is not a stable sorting algorithm in its typical implementation?

A. Merge sort

B. Quick sort

C. Insertion sort

D. None of the above

10. Which of these sorting algorithms is the fastest for sorting small arrays?

A. Insertion sort

B. Quick sort
---------

Insertion Sort Interview Questions and Answers - Sanfoundry

1. 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-place

Answer: a
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.

2. 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 Sort
View Answer
Answer: c
Explanation: 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).

3. The worst case time complexity of insertion sort is O(n2). 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(n2)
c) O(n)
d) O(logn)

Answer: b
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(n2) because of the series of
swapping operations required for each insertion.

Note: Join free Sanfoundry classes at Telegram or Youtube


advertisement

4. Insertion sort is an example of an incremental algorithm.


a) True
b) False

Answer: a
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.

5. Consider the code given below, which runs insertion sort:

Take Data Structure II Practice Tests - Chapterwise!


Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
void insertionSort(int arr[], int array_size)
{
int i, j, value;
for (i = 1; i < array_size; i++)
{
value = arr[i];
j = i;
while (________ )
{
arr[j] = arr[j − 1];
j = j − 1;
}
arr[j] = value;
}
}

Which condition will correctly implement the while loop?


a) (j > 0) || (arr[j − 1] > value)
b) (j > 0) && (arr[j − 1] > value)
c) (j > 0) && (arr[j + 1] > value)
d) (j > 0) && (arr[j + 1] < value)

Answer: b
Explanation: In insertion sort, the element is A[j] is inserted into the correct position in the
sorted sequence A[1… j – 1]. So, condition given in (j > 0) && (arr[j − 1] > value) will implement
while loop correctly.
6. 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 Sort

Answer: d
Explanation: The insertion sort is good for sorting small arrays. It sorts smaller arrays faster
than any other sorting algorithm.

7. 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 1 4 7 9 2 1 2 4 7 9 1 1 2 4 7 9
b) 9 7 4 1 2 9 7 1 2 4 9 1 2 4 7 1 2 4 7 9
c) 7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2
d) 7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9

Answer: a
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
12479
In each step, the key is the element that is compared with the elements present at the left side
to it.

8. 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 false

Answer: b
Explanation: 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.

9. In insertion sort, the average number of comparisons required to place the 7th element into
its correct position is ____
a) 9
b) 4
c) 7
d) 14

Answer: b
Explanation: On average (k + 1) / 2 comparisons are required to place the kth element into its
correct position. Therefore, average number of comparisons required for 7th element = (7 +
1)/2 = 4.
10. Which of the following is not an exchange sort?
a) Bubble Sort
b) Quick Sort
c) Partition-exchange Sort
d) Insertion Sort

Answer: d
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

----------------

Binary search and the method of bisection

==================================================

Q.
Which of these are not types of decrease and conquer
1) decrease by a constant
2) decrease by one
3) decrease by a constant factor
4) variable size decrease

Q.
binary search is the example of...
1) decrease by one example
2) decrease by a constant example
3) decrease by a constant factor example

Q.
topological sort is (select which is wrong)
1) example of decrease by a constant
2) variable size decrease
3) working only if graph is a dag

Q.
sample of variable size decrease is not
1) euclids
2) lomuto partitioning
3) median search
4) source removal

True and false


1. Insertion Sort is better than Selection and Bubble Sort

2.

You might also like