DSA me only

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

DSA me only

DSA

O(1) < O(log n) < O(n) < O(nlog n) < O(n²) < O(2^n)

1.Consider the following sorting algorithm

procedure xSort(var a as array)

if (n=1) return a
var 11 as array = a[0] a[n/2] ...
var 12 as array = a[n/2+1] ... a[n]
11xSort(11)
12xSort(12)
return mergeArrays(11, 12)
end procedure

Predict the name of the sorting algorithmove the following option

Ans : merge sort

2. When two keys result in having the same hash value, the condition is call
Answer: Collision

3.Binary tree
Ans: 305

4.A self-adjusting tree in which the element on which operation is performed becomes the root
of the tree
Answer : Splay Tree

5.A B tree that stores data on leaf nodes and keys in internal nodes is known as
Answer : B+ Tree

6.The least time complexity of removing duplicate from the unsorted linked list
Answer: O(n)
The least time complexity for removing duplicates from an unsorted linked list is O(n), where n is
the number of elements in the linked list. This can be achieved by using a hash set to keep track
of unique elements while traversing the linked list.
8.There are two data structures, one an array and another a singleked list Both stores same set
of elements. Let A indicate time taken to search in an array,B indicate to search in a linked list.
Which of the following is true on a typical modern computer.
Answer: A< B
On a modern computer, searching in an array is generally faster than in a linked list due to
better cache efficiency and potential use of binary search. Hence, A < B.

10.Which of the following is not a self-balancing tree?

Red-black trees
AVL Trees
2-3 Trees
None of the above
Ans:- None of the above

11.Fill in the blank with the most appropriate option:


Jack was given two non-negative functions f1 and f2. The function f1 (n) =O(f2(n)) if and only
if there exist a positive constant K and no such that?
answer:
a) f1(n) ≤ K*f2 (n)
b) f1(n) = K*f2(n)
c) f1(n) ≥ K*f2(n)
d) f1(n) 1 = K*f2(n)
Ans:-a) f1(n) ≤ K*f2 (n)

12.Sam has a problem K. The problem K is NP-complete if K is NP-hard. Which of the following
conditions satisfy this?
a) K:NP
b) K directly proportional to NP
c) KENP
d) K=NP
Answer: d) K=NP

13.A hash table has 12 buckets and uses linear probing to insert values. The hash function is K
mod 8 and the key values are combined. The elements 11, 17, 14, 21, 31, 28, 51, 18 were
inserted into the hash table in that order, which location holds the 18.
a) 1
b) 4
c) 7
d) 2
Answer: 2

14. Thomas has written an algorithm to sort an array. The algorithm is given below.
Thomas to get the running time required for this algorithm.
list: array of items
n: size of list
for i = 1 to n - 1
min=1 for j = 1 + 1 to n if list[j] < list[min] then min - 33
end if end for
if indexMin 1 = i then swap list[min] and 1ist[1] end if end for
end procedure
a) O(log(2n)) (logbase2)
B) O(n10g * 2n) logbase2)
c) O(n)
d) O(n ^ 2)
Answer: d) O(n ^ 2)

15.what will be the outcome of the below algorithm:


1. Begin from the first element, then perform a comparison with the following element in the
array.
2. If the present element is larger than the following element of the array, then swap their
positions.
3. If the present element is lesser than the following element of the array, shift to the next
element, and again repeat step 1. I
Searching a particular
a) Reversing the given list
b) Sorting the list of given numbers
c) Addition of first N Numbers
d) searching a particular
Answer: b) Sorting the list of given numbers

16.What is the complexity of a binary search algorithm?


a) O(log n).
b) O(n log n)
c) O(n)
d) O(n^2)
Answer: a) O(log n)

17. In linear search algorithm, the worst case manifests in which of the following situations?
a) When item is the last element in the array
b) When item is the last element in the array or When item is not in the array at all
c) None of the given options
Answer: b) When item is the last element in the array or When item is not in the array at all

18. Which of the following solutions would you get when you apply Prim's algorithm on a graph?
a) Traveling salesman problem
b) Graph coloring problem
c) Transitive closure of a graph
d) Minimum cost spanning tree
Answer: d) Minimum cost spanning tree

19. As a schedular, Operating System has to schedule different jobs that are coming for
processing. OS has to refine the precedence of each job for scheduling. Which queue is used
for storing and scheduling these jobs?
a) Linear Queue
b) Circular Queue
c) Dequeue
d) Priority Queue
Answer: d) Priority Queue

20. you want to store the following data of N students and porform the operations like searching
and sorting Which of the following you will use for the same?
Answer: Array of Structures.

21. Which of the following solutions would you get when you apply Prim's algorithmon a graph?
Answer: Minimum Spanning Tree (MST)

22. Which one of the following is an application of Stack Data Structure?


a) Managing function calls
b) The stock span problem
c) Arithmetic expression evaluation
d All of the above
Answer: d) All of the above

23. Which one of the following is an application of Queue Data Structure?


a) When a resource is shared among multiple consumers.
b) When data is transferred asynchronously (data not necessarily received at same rate as
sent) between two processes
c) Load Balancing
d) All of the above
Answer : d

24.Which of the following sorting algorithms can be used to sort a random linked list with
minimum time complexity?
a) Insertion Sort
b) Quick Sort
c) Heap Sort
d) Merge Sort
Answer: d) Merge Sort
25. Which of the following is true about linked list implementation of stack?
a) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
b) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
removed from the beginning.
c) Both of the above
d) None of the above
Answer: d) None of the above

26. Which of the following is an advantage of adjacency list representation over adjacency
matrix representation of a graph?
a) In adjacency list representation, space is saved for sparse graphs.
b) DFS and BSF can be done in O(V+ E) time for adjacency list representation.
These operations take O(V^2) time in adjacency matrix representation. Here is
V and E are number of vertices and edges respectively.
c) Adding a vertex in adjacency list representation is easier than adjacency matrix
representation.
d) All of the above
Answer: d) All of the above

27. Which of the following sorting algorithms follows the divide-and-conquer approach?
a) Insertion sort
b) Bubble sort
c) Quick sort
d) None of the above
Answer: c) Quick sort

28. A binary tree can be traversed in


a) Inorder
b) Preorder
c) Postorder
d) All of the above
Answer: d) All of the above

29. Stack works on the principle of


a) Last In First Out
b) Last In First Out
c) First In Middle Out
d) None of the Above
Answer: a) Last in First Out

30. When determining the efficiency of an algorithm, how would you measure
the space factor?
a) By counting the maximum memory needed by the algorithm
b) By counting the minimum memory owned by algorithm
c) By counting the average memory needed by the algorithm
d) By counting the maximum disk space made by the the algorithm
Answer: a) By counting the maximum memory needed by the algorithm

31. What is the complexity of a bubble sort algorithm?


a)O(log n)
b) O(n log n)
c) O(n^2)
d) O(n)
Answer: c) O(n^2)

32. You want to store n integer numbers inputed by the user and perform the actions like
searching and sorting on those numbers. Which of the following
you will use to store the data?
a) Structure
b) Union
c) Variable
d) Arrays
Answer: d) Arrays

33. The common term used to adding data item to a stack.


a) Insert
b) Commit
c) Push
d) None of the above
Answer: c) push

34. what an extra key inserted at the end of the arrayis called?
a) Transposition
b) Stop key
c) Sentinel
D) Not sure
Answer: c) Sentinel

35.Based on the pseudo code, which scheduling algorithm is being used?


While there are processes to be executed:
Select the process with the smallest execution timeExecute process
Answer
First-Come, First-Served (FCFS)
Shortest Job First (SJF)
Round-Robin (RR)
Priority Scheduling
Ans : SJF

36.Which of the following statement is TRUE about binary insertion sort?


Answer
Binary search is used to reduce the number of comparisions.
Binary tree is used in its implementation
Linear search is used to reduce the number of swaps
None of these
Ans: A. Binary search is used to reduce the number of comparisons.

37.Consider an array of integers a= (1,1,1,7,8,9,16,21).


Calculate the sum of those elements of the array whose square root is an integer.
Answer : 28 (1+1+1+9+16)
20
35
28
29

38.Which of the following statements is true?


1) A hash function takes an input (or 'message') of any length and generates a fixed-length
code.
2 A hash function takes a message of a fixed length and generates a code of variable length
3) A hash function may produce the same hash value for distinct messages.
Answer : 1 & 3. Not confirmed
1 only
1 and 2
1 and 3
2 only

39. What will be the output of the following code snippet if head is pointing to the first node of
the given linked list 6->7->8->9->10->11
void printx(struct node head)
{ if(!head)
return;
printf("%d ", head->data);
if(head->next){
printx(head->next->next);
printf("%d ", head->data);
}
}

Answer: B. 6 8 10 10 8 6
11 10 9 8 7 6
6 8 10 10 8 6
40.Stack is implemented using an array arr= (1, 4, 3, 6, 5), if for
every odd number one even number is popped, and if an even number does not exist, then the
odd number is not popped,
which element will be at TOP in the end?
Answer : 1 not confirmed
6
3
4
1

41.Consider an array representation of non circular queue, Q={_,3,3,2,2,2,2,_,_,_,_,_,_}


What is the maximum number of times the en queue operation can be performed, after which
overflow condition will occur?
Assume one-based indexing.
Answer : 7
5
5
7
1

42.You are given a graph of 5 nodes. How will you detect whether the graph is also a tree?
Answer: b.if there are not one or more cycles in the graph.

if the number of nodes is less than 8


if there are not one or more cycles in the graph.
if the graph is a directed graph.
if the graph has 0 or more incoming nodes.

43.What will strcmp() function returns when two strings are identical?
Answer : 0
1
-1
0
True

44. Consider the following statements:


i. Stacks efficiently support first-in-first-out types of computations.
ii. Implementing lists on linked lists is generally more efficient than implementing lists on an
array for most basic list operations.
iii. Implementing queues on a circular array is more efficient than implementing queues on a
linear array with two indices.
iv. Queues efficiently support last-in-first-out types of computations.
Which of the above statements is correct?
Answer : 2&3 are true
(Please choose a correct answer)
(1) and (2) are true
(2) and (iii) are true
(3) and (iv) are true

45. Given an array that contains the numbers 1 through 8 in ascending order, identify the
longest subsequence where the product of its elements, when divided by 8, yields a reminder of
exactly 1
Answer : 1 3 5 7
1357
12357
1246
1568

46.What is the minimum time complexity for removing duplicates from an unsorted linked list?

Answer : O(n)
O(n ^ 3)
O(n ^ 4)
O(n)
None

47. data [1, 5, 8, 2, 10, 3, 7]


result=0
for i in range(len(data)):
for j in range(i+1, len(data)):
if data[i] > data[j]:
result + data[j]
else:
result + data[i]
After the code execution, what will be the value of "result"?
Answer : 65

48.count = 0
for i in range(3):
for j in range(3):
count += i+ j
After the code execution, what will be the value of 'count?
Answer : 18
6
14
18
24

49.Which of the following data structure can be used for performing undo and redo operations?
Answer: stack
Tree
Graph
Stack

50. What will the code do..


int solve(Queue *Queue)
{
Stack st;
// Step 1: Transfer all elements from the queue to the stack
while (!isEmpty(Queue)) // Check if the queue is not empty
{
push(&st, deQueue(Queue)); // Remove the front element from the queue and push it onto
the stack
}

// Step 2: Transfer all elements back from the stack to the queue
while (!isEmpty(&st)) // Check if the stack is not empty
{
enQueue(Queue, pop(&st)); // Pop the top element from the stack and enqueue it back to
the queue
}
}
Answer : D. Reversea the queue
Removes the last from Queue
Keeps the queue the same as it was before the ca
Makes the queue empty
Reverse the Queue

51. Consider the following statements about the Insertion Sort algorithm:
1. When the elements are already sorted, it takes an order of n time to complete
2. It is a stable sorting technique.
3. it is typically used when the number of elements is extremely large.
Which of the above statements are true?
Answer : 1&2 are true

52.Consider the following pseudo-code that uses a stack.


declare a stack of characters
while (there are more characters in the word to read)
{read a character
push the character on the stack
}
while (the stack is not empty)
{pop a character off the stack
write the character to the screen
}
What is the output for input "doselectquiz”
Answer: ziuqtcelesod

doselectau izdoselectquiz
ziuatcelesod
poselectquiz
53.

10
12
16
9

54.The steps to write a function in C language that adds a single new node at the start of any
linked list are given in a jumbled order. Select the option that denotes the correct sequence of
steps.
A. Make the first pointer point to the new node.
B. Allocate a new node from the heap.
C. Store the required data in data element of the node.
D. Make the new node point to the current first node of the list.

Answe : B C D A
ABCD
CBDA
BCDA
55.

Answer: A. It will Merge 2 sorted arrays

56.Given a binary max heap that contains 'n' numbers, what is the time complexity to find the
smallest element?
Answer : O(n)
O(logn)
O(n)
O(loglogn)
O(1)

57.If the stack is implemented using an array arr = (2, 5, 13, 4, 15, 6, 7, 8, 9) (0-based indexing)
Which element would be at the top of stack, if the last element of the array is inserted first in the
stack?
Answer: 2
13
5
9
2

58.Which sorting algorithm is the most efficient for large datasets with random order?
Answer : Quick Sort
Bubble Sort
Selection Sort
Quick Sort
insertion Sort

59.Bran made a full binary tree. If the binary tree contains 12 numbers of internal nodes, how
many leave nodes does the binary tree contain
Choose from the following options.
Answer :
24
11
13
25

60.Which algorithm is primarily used for traversing trees?


Answer : Depth-First Search (DFS) algorithm
Dijkstra's Algorithm
Kruskal's Algorithm
Prim's algorithm
Depth-First Search

61. Consider a stack of integers. The stack is allocated 5 memory cells.


STACK: 3, 8, 1, 5,_.
The following operations are performed on the stack-
1) POP(STACK, ITEM1)
2) PUSH (STACK, ITEM1)
3) POP(STACK, ITEMI)
4) PUSH (STACK, ITEM1)
5) POP(STACK, ITEM1)
6) PUSH (STACK, ITEM1)
7) POP(STACK, ITEM1)
8) PUSH (STACK, ITEM1)
9) POP(STACK, ITEM1)
10) PUSH (STACK, ITEM1)
11) POP(STACK, ITEM1)
12) PUSH (STACK, ITEM1)
13) POP(STACK, ITEM1)
14) PUSH (STACK, ITEM1)
Identify the topmost element of the stack after all the operations are performed.
POP(STACK, x)
means topmost element is popped from stack and stored in x.

Answer: 5

62.Which type of algorithm uses a priority queue?


Answer: Dijkstra's algorithm
DFS
Bfs
Dijkstra's Algorithm
Kruskal's Algorithm

63.Which of the following statements is true for Binary Trees?


Answer:

Keeps keys in sorted order for sequential traversing


Uses a hierarchical index to minimize the number of disk reads
Uses partially full blocks to speed insertions and deletions
None of the above

64.Stephen has a linked list composed of 6 nodes that he wishes to sort. Given the sorting
techniques of Quick Sort and Merge Sort, which one would be the most suitable for his needs?
Answer: 1. (Merge sort most suitable)

65.Consider a node X in a Binary Tree. Given that X has two children, let Y be inorder
successor of X. Which of the following is true about Y?
Answer : B.Y has no left child
Y has no right child
Y has no left child
Y nas both children
None of the above

66. What will happen if you try to access arr[5]?


int[] arr = (2, 4, 6);
System.out.println(arr[5]);

Ans: C.ArrayIndexOutOfBoundsException
NullPointerException
0
ArrayIndexOutOfBoundsException
it prints nothing
67.What is the lifetime of a dynamically allocated variable?
Answer : B.until programs ends
Until scope end
until manually deallocated
Until program ends
Until function returns

68. Consider the following statements:


1. A Breadth-first search is conducted using a stack.
2. A Depth-first search is conducted using a queue.
Which of these statements, if any, are true?
Answer : D. Neither 1 nor 2
(Please choose a correct answers)
1 only
2 only
Both 1 & 2
Neither 1 nor 2

69.If a stack of N elements is implemented with only one queue, one pop operation is equal to
how many enqueue and dequeue operations in general?
Answer : N-1 enqueue, 1 dequeue
(Please choose answer)
N-1 enqueue. N dequeue
1 enqueue, N-Idequeue
1 enqueue, 1 dequeue
N-1 enqueue, 1 dequeue

70. The enqueue() method is used to add an element to a queue whereas the deque() method
is used to remove an element from the queue.
What is the output of the following pseudocode?
QUEUE q
q.enqueue(9)
q.enqueue(4)
q.deque()
q.enqueue(2)
q.deque()
q.enqueue(6)
q.enqueue(12)
PRINT q.front()

Ans : 2
71. What is the complexity of Dijkstra's Algorithm? No
Answer :
(Please choose a correct answer)
O(E + V logE)
O(V logE)
O(V + ElogV)
0(E logV)

72. Aman likes to solve challenging problems. Once his friend gave him two arrays of the same
length. His task is to find the value of two summations
Z=Σ Σ max(Ai+Bj, Bi+Aj)
A=[1,2,4,3]
B=(2,3,8,1]]

Answer : 114
(Please choose a correct answer)
120
114
130
None of these

73. How many subarrays have to be searched to find the element 0 in the given below array
when the Binary search algorithm is applied?
A = (0,1,2,3,4,5,12,13,15)
Answer :
(Please choose a correct answer)
2
3
1
0

74.Consider a sorting technique referred to as 'X'. The following characteristics are known about
'X':
1. 'X' is not a comparison-based sorting technique.
2. The running time complexity of 'X' is O(n).
3. The space-time complexity is proportional to the range of the data.
Based on these characteristics, which sorting technique could 'X' possibly be?
Answer : C.Count sort
(Please choose a correct answer)
Bubble sort
Insertion sort
Count sort
Merge Sort
75.

76.Consider the below algorithm. What is the error in the logic for the given binary search ?
1. Set L to and R ton - 1.
2. If L < R, the search terminates as unsuccessful.
3. Set m (the position of the middle element) to the floor (the largest previous integer) of (L +
R)/2.
4. If Am <T, set L to m + 1 and go to step 2
5. If Am > T, set R to m1 and go to step 2.
6. Now Am T, the search is done; return m.

Answer : In line 2, L should be greater than R for array arranged in descending order
(Please choose a correct answer)
In line 2, L should be greater than R
In line 3, the floor ((L+R)/2) should be (L+(L+R))/2)
In line 2, L should be greater than R for array arranged in descending order
None
77.Suppose we are sorting an array of eight integers using heapsort, and we have just finished
some heapify (either maxheapify or minheapify) operations. The array now looks like this
16 14 15 10 12 27 28
How many heapify operations have been performed on root of heap?

Answer : 3 or 4
Please choose a correct answer)
1
2
3 or 4
5

78.Stack is implemented using an array arr=(11, 12, 13, 14, 15) of size 5, what would be an
index of TOP if 16 is to be pushed? (assume 0-based indexing
Answer: C. No element can be pushed
15
11
No element can be pushed.
None of the Above
79.

Answer: 9 6 3

80.Bran made a full binary tree. If the binary tree contains 12 numbers of internal nodes, how
many leave nodes does the binary tree contain.
Choose from the following options.
Answer: 13. L = i+1
Please choose a correct answer)
24
11
13
25

81. What holds function parameters and return addr?

Answer : Stack
(Please choose a correct answer)
Heap
Stack
Text
Data

82. Can a function have no parameters and still return a value?


Answer: yes

(Please choose a correct answer)


No
Yes
Depends on language
Only for built-ins

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy