Computer Knowledge

Data Structures and Algorithms

1,256 Questions

Data Structures and Algorithms form the core of computer science, focusing on arrays, linked lists, trees, and sorting mechanisms. These concepts are essential for solving complex computational problems efficiently. Test takers preparing for technical and administrative IT exams will find these questions highly relevant.

Array OperationsLinked List ApplicationsSorting AlgorithmsTree Data StructuresMultilevel IndexingAlgorithm Time Complexity

Data Structures and Algorithms Questions

Multiple choice

Consider the following algorithm for searching for a given number x in an unsorted array A[1.....n] having n distinct values : (1) Choose an i uniformly at random from 1....n If A[i] = x then stop else Goto 1;

Assuming that x is present A, What is the expected number of comparisons made by the algorithm before it terminates?

  1. n

  2. n -1

  3. 2n

  4. $\dfrac{n}{2}$
Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Given array A[1.....n], an element A[i] is chosen randomly from 1 to n.This would require n selections & comparisons to find x in array

Multiple choice
  1. 1 only

  2. 2 and 3

  3. 3 only

  4. 4 only

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

For a tree we not only require in order & preorder but also post order traversal. Preorder & post order help to determine the roots of binary subtrees, inorder arranges those roots in order.

Multiple choice
  1. best if A is in row-major, and B is in column-major order

  2. best if both are in row-major order

  3. best if both are in column-major order

  4. independent of the storage scheme

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Since the matrices are stored in array, there is no dependence of time complexity on row major or column major. Here only the starting address is known & on the basis of indexes the next memory locations are calculated.

Multiple choice
  1. union only

  2. intersection, membership

  3. membership, cardinality

  4. union, intersection

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Membership & cardinality functions takes constt. time i.e. 0(1), but union & intersection require emparison of 1 element with all the other elements so these two would be slowest.

Multiple choice
  1. $\theta$(n)
  2. O(nlogn)

  3. O(n)2

  4. O(2n)

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The function g(x) calculates for a node x min no. of leaf noes whether in left subtree or right subtree. The for balanced BST. The no. of inner nodes = leaf nodes−1. So this loop will sun for max n-times so complexity is  $\Theta$(n)