Multiple Choice Questions on Data Structures with Answers

Overview of Data Structures and Algorithms

Data structures are essential entities in programming used to store data in an ordered format that allows for efficient processing. These structures can be primitive, such as int, bool, and float, or abstract, such as Tree and LinkedList. They can also be linear, such as Arrays, or non-linear, such as LinkedList and Trees.

Algorithms, on the other hand, are well-defined sets of instructions written to accomplish a specific task. They are not the program itself but a series of steps that embody the program's underlying logic, following which the working program can be coded down. Algorithms must have inputs and outputs, as well as finiteness, definiteness, and correctness properties. Efficiency is measured on various parameters such as Time Complexity and Space Complexity.

While there are countless algorithms, some of the most commonly used examples include flows, lowest common ancestor of nodes, sorting, and searching.

Multiple Choice Questions on Data Structures and Algorithms

1. Which of the following is not a primitive data structure?
A. int
B. bool
C. float
D. Tree

2. Which of the following is not an abstract data structure?
A. Tree
B. LinkedList
C. Queue
D. int

3. Which of the following is an example of a linear data structure?
A. Tree
B. LinkedList
C. Graph
D. Arrays

4. Which of the following is an example of a non-linear data structure?
A. Queue
B. LinkedList
C. Arrays
D. Trees

5. Which of the following is not a property of algorithms?
A. Input
B. Output
C. User-friendliness
D. Finiteness

6. How is the efficiency of an algorithm measured?
A. Time Complexity
B. Space Complexity
C. Both A and B
D. Neither A nor B

7. What is the lowest common ancestor of nodes?
A. An example of a primitive data structure
B. A sorting algorithm 
C. An example of an abstract data structure
D. An example of an algorithm

8. What is sorting?
A. A primitive data structure
B. An example of an abstract data structure
C. A non-linear data structure
D. An algorithm

9. What is searching?
A. An algorithm
B. A linear data structure
C. A non-linear data structure
D. An abstract data structure

Initializing Arrays in C Language

In C language, an array can be initialized using the following syntax:


int a[3] = {1, 2, 3};

Here, the array 'a' is being initialized with three elements: 1, 2, and 3.

Option (B) is incorrect because it tries to initialize an integer variable 'a' with curly braces, which is not allowed in C.

Option (C) is incorrect because it uses a new operator, which is not supported for initializing arrays in C.

Option (D) is incorrect because it uses parentheses instead of square brackets to enclose the size of the array, and it uses square brackets instead of curly braces to initialize the array elements.

Therefore, option (A) is the correct way to initialize an array in C.

Linear Data Structures and Example

A linear data structure is a data organization format where the data items are organized sequentially. The arrangement of elements in this type of structure is linear, from start to end. A few examples of linear data structures include:

  • Arrays
  • Linked lists
  • Stacks
  • Queues

Of the options provided, the array is the only linear data structure. AVL trees, binary trees, and graphs are non-linear data structures.

// example array in Python
numbers = [2, 4, 6, 8, 10]
print(numbers) // Output: [2, 4, 6, 8, 10]

Accessing the 2nd Element in an Array using Pointer Notation

In pointer notation, the expression to access the 2nd element of an array is *(a + 1). The reason why 1 is used instead of 2 is because the index of the array starts at 0.

Here is an example of how to use this notation to access the 2nd element in an array:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Assign pointer to first element of array

// Accessing second element of array using pointer notation
int secondElement = *(ptr + 1);

// Printing second element of array
printf("Second element of array is: %d", secondElement);

The output of the code above will be:

Second element of array is: 2

It is important to note that the expression *a + 2 is not valid for accessing the 2nd element of an array using pointer notation. This expression will add 2 to the value pointed to by a and then dereference that value, which is not the intended behavior.

Types of Queues

//Single ended queue implementation
class SingleEndedQueue {
    //code for Single Ended Queue

//Ordinary queue implementation (FIFO)
class OrdinaryQueue {
    //code for Ordinary Queue

//Priority queue implementation (elements with high priority are dequeued first)
class PriorityQueue {
    //code for Priority Queue

//Circular queue implementation
class CircularQueue {
    //code for Circular Queue

The correct answer is B) Single Ended Queue is not a type of queue.

Identifying Non-Data Structure Operations

In data structures, operations are functions or methods that are implemented to perform certain tasks on the data that the structure holds. These operations can manipulate the data, perform computations, monitor events, and more. However, some operations are not related to data manipulation and are not considered as part of data structure.

The operation that is not related to data structure is C) Operations that check for syntax errors. This operation is usually related to programming language syntax and checking for errors in code rather than manipulating data.

As the code snippet is incomplete, it is not possible to determine the output. The code will result in a compilation error as the for loop is not complete. The code is missing the condition for the for loop to end and also the closing curly braces for the function.I'm sorry, but the given code snippet appears to be incomplete as there is no closing curly brace for the `solve()` function and the end of the `for` loop is missing. Please provide the complete code snippet so I can accurately answer your question.

Disadvantage of Array Data Structure

One major disadvantage of array data structure is the need to allocate memory for the entire array beforehand. This means that the size of the array must be known in advance, which can be a limitation in certain situations. Additionally, arrays are stored in contiguous memory blocks, which can make it difficult to insert or remove elements in the middle of the array. Despite these drawbacks, arrays are still a valuable data structure due to their ability to provide constant time access to individual elements and their versatility in implementing other data structures.

// Example code showing how to initialize an array in C++
int arr[5] = {1, 2, 3, 4, 5};

Memory Representation of Strings in C

In C, strings are represented as an array of characters. Each character in the string is stored in the array as a single element. The end of the string is denoted by a null character ('\\0') which marks the end of the string.

For example, if we have a string "hello", it would be stored in memory as:

{'h', 'e', 'l', 'l', 'o', '\0'}

This array of characters is commonly called a C-string and is terminated by the null character.

To access characters in the array, we can use indexing just like in any other array. For example:

char str[] = "hello";
printf("%c\n", str[0]); // prints 'h'
printf("%c\n", str[1]); // prints 'e'
printf("%c\n", str[4]); // prints 'o'

Note that strings in C are not objects of a class like in some other programming languages. They are simply an array of characters.As the provided code snippet is incomplete, it is not possible to determine the output or purpose of the code. The code snippet is missing the end of the for loop and function "solve()". Please provide the complete code snippet.

Advantages of the Array Data Structure

The advantage of the array data structure is that its elements are stored in a contiguous block of memory, which makes it easier to access the elements in an array. This means that we can access any element of the array directly by its index number.

In contrast, elements of mixed data types cannot be stored in an array. Additionally, the index of the first element in an array starts from 0 rather than 1. It is also possible to sort elements of an array, contrary to the statement made in option D.

Appending a Character at the End of a String in C++

The correct function used to append a character at the back of a string in C++ is the


function. It adds a new character to the end of the string. Here's an example:

#include <string>
#include <iostream>

int main() {
   std::string str = "Hello";
   std::cout << str << std::endl; // Output: Hello!
   return 0;

In the above code, a new character '!' is added to the end of the string using the


function. The output is "Hello!".

Application of Queue Data Structure

There are various applications of the queue data structure:

  • When a resource is shared among multiple consumers.
  • When data is transferred asynchronously.
  • Load Balancing.

Therefore, option D, which states that all of the above are applications of queue data structure, is correct.

// No code to update here as it is a plain text answer

Underflow condition when calling pop() on an empty queue

When the pop() operation is called on an empty queue, it leads to the underflow condition. This condition arises when you try to remove an element from an empty data structure like a stack or a queue.

//Implementation of pop() operation for a queue

int dequeue() {
   if(isEmpty()) {
      printf("Underflow condition occurred!");
      return INT_MIN;
   else {
      int element = queue[front];
      front = (front + 1) % MAXSIZE;
      return element;

The above code checks whether the queue is empty or not and returns the minimum value of the data type in case of underflow condition. Otherwise, it dequeues the element from the front of the queue.

Title: Time Complexity of Code Snippet in C++


The code snippet provided does not contain the complete code and hence it's difficult to determine the time complexity. Incomplete code makes it challenging to predict the exact number of iterations, conditions, and operations involved.

For instance, the complexity of the for loop is not known. It'd be safe to assume that the time complexity of the given code snippet should be O(1), considering that the operations and conditions inside the for loop are constant.

In general, the time complexity is a measure of the algorithm's performance, and it determines the amount of time required to execute an algorithm. It can be measured by counting the number of operations performed by the algorithm and the size of the input data.

To optimize time complexity, it is essential to use efficient algorithms, avoid excessive loops, reduce the number of recursion, and use efficient data structures.

Data Structures for Implementing Queues

Queues can be implemented using stack, arrays, and linked list.


class Queue:
    def __init__(self):
        Create a new empty Queue.
        self.items = []

    def is_empty(self):
        Check if the Queue is empty.
        return len(self.items) == 0

    def size(self):
        Returns the size of the Queue.
        return len(self.items)

    def enqueue(self, item):
        Add a new item to the back of the Queue.

    def dequeue(self):
        Remove and return the front item of the Queue.
        if self.is_empty():
            return None
        return self.items.pop(0)

The Queue class is implemented using an array. Other data structures such as a linked list or a stack could also be used to implement it.

Recursion and Data Structures

The data structure that finds its use in recursion is a stack.

A stack is a data structure that works on a Last-In-First-Out (LIFO) basis. In recursion, we keep on putting function calls onto the stack until we reach the base condition or the termination condition, after which we start popping off function calls from the stack to complete the execution. This helps us in preventing the overflow of the call stack and also keeps the function calls in order.

    def factorial(num): 
        if num == 1: 
            return 1 
            return num * factorial(num-1) 

Here, we are using recursion to calculate the factorial of an integer. Each function call is pushed onto the stack and when the base case is reached, the function calls are popped off the stack to calculate the result.

Data Structures allowing Insertion and Deletion from both Ends

Among the given data structure options, only the Deque (Double-Ended Queue) allows insertion and deletion from both ends. Stacks and Queues are restricted to one end, while Strings are not considered data structures for this purpose. Therefore, the correct answer is Deque.Unfortunately, the code snippet provided is incomplete. It ends with "for(int i = 1; i ", without closing the statement with curly braces or semicolon. Without the rest of the code, it is impossible to determine what the output will be.

Optimal Sorting Algorithm

In the given options, Merge Sort is the optimal sorting algorithm in terms of time complexity. It provides O(n * logn) time complexity in the worst case scenario, which is better than Quick Sort, Bubble Sort, and Selection Sort. Therefore, Merge Sort is a better choice for sorting large data sets as it ensures efficiency and performance.

Maximum Swaps in Selection Sort Algorithm

The maximum number of swaps that can be performed in the selection sort algorithm is n - 1, where n represents the number of elements in the array. This is because each pass of the algorithm selects the minimum element and swaps it with the element at the beginning of the unsorted portion of the array. Since there are n-1 unsorted elements in the array after the first pass, there can be at most n-1 swaps to sort the entire array.

// Selection Sort Algorithm

function selectionSort(arr){
   for (let i = 0; i < arr.length - 1; i++){
      let minIndex = i;
      for (let j = i + 1; j < arr.length; j++){
         if (arr[j] < arr[minIndex]){
            minIndex = j;
      if (minIndex != i){
         let temp = arr[i];
         arr[i] = arr[minIndex];
         arr[minIndex] = temp;
   return arr;

Identifying Divide and Conquer Algorithm

Merge Sort is an example of a Divide and Conquer algorithm.

// Implementation of Merge Sort
function mergeSort(array) {
    if (array.length <= 1) {
        return array;

    const middle = Math.floor(array.length / 2);
    const left = array.slice(0, middle);
    const right = array.slice(middle);

    return merge(

function merge(left, right) {
    let resultArray = [], leftIndex = 0, rightIndex = 0;

    while (leftIndex < left.length && rightIndex < right.length) {
        if (left[leftIndex] < right[rightIndex]) {
        } else {

    return resultArray

Choosing the Best Sorting Algorithm for Small Array Elements

When dealing with small arrays, the most efficient sorting algorithm is usually Insertion Sort. This is because the overhead of more complex algorithms, such as Merge Sort or Quick Sort, outweighs the benefits when dealing with small data sets. Insertion Sort has a time complexity of O(n^2), which may not be the best for large arrays, but is very effective and practical for small arrays.

Here is an implementation of Insertion Sort in Python:


def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr

This implementation sorts the input array `arr` by iterating through each element and comparing it to the previous elements until it reaches the beginning of the array or finds an element smaller than itself. It then swaps the two elements and continues until the end of the array is reached.

Overall, Insertion Sort is a great choice when working with small arrays due to its simplicity and ease of implementation.

Applications of Topological Sort

Topological Sort is a useful algorithm for numerous applications:

  • Sentence ordering: This algorithm can be used to put a jumbled-up set of sentences in the correct order.
  • Course scheduling: This can be used to create a schedule for courses, where prerequisites must be taken before specific courses.
  • OS Deadlock Detection: This algorithm is used to facilitate deadlock detection in Operating Systems.

Thus, the correct option is: All of the above.

Identifying an NP-Hard Problem

In the given options, the 0/1 Knapsack Problem is the only problem that is not an NP-Hard problem. This problem is usually solved using dynamic programming.

Please note that identifying an NP-Hard problem can be quite challenging and requires deep knowledge of computational complexity theory. NP-Hard problems are problems that are at least as hard as the hardest problems in NP. The Knapsack Problem, on the other hand, has a polynomial-time solution and is therefore not NP-Hard.

Algorithms for String and Pattern Matching

In computer science, string matching problems involve finding a substring of a given string that matches a given pattern. There are various algorithms that can be used to solve string matching problems efficiently. The following algorithms are commonly used:

  1. Z Algorithm
  2. Rabin-Karp Algorithm
  3. KMP Algorithm

All of the above algorithms are used for string and pattern matching.

Calculating distance between nodes using Lowest Common Ancestor

Given a function named `getLCA()` which returns the Lowest Common Ancestor between two nodes in a tree and the distance from the root to each node is already calculated.

To calculate the distance between the two nodes, we can use the formula:

`dist(u) + dist(v) - 2 * dist(getLCA(u, v))`

Here, `u` and `v` are two nodes and `dist()` represents the distance from the root of the tree to a particular node.

We subtract twice the distance of the Lowest Common Ancestor from the sum of distances from the root to the two nodes to get the distance between the two nodes.

This is because the path from the root to the Lowest Common Ancestor is common to both nodes and hence is counted twice. Therefore, to get the actual distance between the two nodes, we need to subtract twice the distance of the Lowest Common Ancestor.

Thus, option (A) is the correct answer.

Algorithm for Processing Tree Queries

The two algorithms which are commonly used for processing queries on trees are Centroid Decomposition and Heavy Light Decomposition. Both of them have their own strengths and weaknesses and can be used depending on the type of problem.

Centroid Decomposition is used to divide a tree into smaller components, known as centroids, in such a way that the maximum size of any centroid is less than or equal to half of the size of the original tree. This makes it easier to handle queries on the tree, as we can process them on each centroid separately.

Heavy Light Decomposition is another algorithm that is useful for handling queries on trees. It works by dividing the tree into linear chains, each of which has at most log(n) nodes, where n is the total number of nodes in the tree. The linear chains are processed separately, and the results are combined to get the final answer to the query.

Therefore, the correct answer is option (C) Both (A) and (B).Title: Code Output Prediction


void solve() {
    vector<int> a = {1, 2, 3, 4, 5};
    sort(a.begin(), a.end(), [&](const int &x, const int &y) {
        return x % 2 < y % 2;

    for (int i = 0; i < a.size(); i++) {
        cout << a[i] << " ";
    cout << endl;

int main() {
    return 0;

The output of the given code snippet will be `1 3 5 2 4`.

This is because the `sort` function is sorting the vector `a` based on the remainder of each element after being divided by 2, with odd numbers appearing before even numbers. The lambda function `(const int &x, const int &y) { return x % 2 < y % 2; }` is responsible for this sorting. After sorting, the elements of `a` are printed in order using a `for` loop and then a newline character is added to the output using `cout << endl;`.Code:

Vector Update Function

This code snippet updates a vector based on given queries. Each query can be of two types:
1. Update an element of the vector at a given index with a given value.
2. Print the sum of all elements in a sub-vector defined by given indices.

// Function to solve queries and update vector based on that.
void solve(vector<int>& vec) {
   int queries;
   cin >> queries;
   while(queries--) {
       int type;
       cin >> type;
       if(type == 1) {
           int index, value;
           cin >> index >> value;
           // Update vector element at given index with given value.
           vec[index] = value;
       else {
           int l, r;
           cin >> l >> r;
           int sum = 0;
           // Calculate sum of elements in sub-vector defined by l and r.
           for(int i=l; i<=r; i++) {
               sum += vec[i];
           // Print the sum.
           cout << sum << endl;


The time complexity of the binary search algorithm is O(log2n) because in each iteration, the search space is reduced by half. Therefore, it takes logarithmic time to search an element in a sorted array using the binary search algorithm.

Kruskal's Algorithm for Finding the Minimum Spanning Tree of a Graph

Kruskal's Algorithm is a type of Greedy Algorithm used to find the Minimum Spanning Tree (MST) of a graph. The Greedy Algorithm operates by selecting the edges with the lowest weight unless it forms a cycle. This algorithm is not a DP problem or ad hoc problem. Therefore, the answer is option B, Greedy Algorithm.This code snippet will give an error because the while loop is not complete, and the code is incomplete. It is missing the closing bracket for the "while" loop and the end bracket for the "solve" function.

Implementation of Maps in C++

// C++ code to demonstrate the implementation of Maps using Red-Black Trees
#include <iostream>
#include <map>
using namespace std;

int main() {
   // declare map
   map<int,string> Map;

   // Insert elements in Map using insert() function

   // Print the Map using iterator
   map<int,string>::iterator it;
      cout<<it->first<<" "<<it->second<<endl;

   return 0;

In C++, Maps are implemented using Red-Black Trees which provide efficient and balanced searches for the stored elements. Maps can be declared using the 'map' keyword and elements can be inserted using the 'insert()' function. The 'iterator' is used to traverse and print the Map.


#include <iostream>

using namespace std;

void solve() {
    int n = 24;
    int l = 0, r = 100, ans = n;
    while (l <= r) {    // Changed '1' to 'l'
        int mid = (l + r) / 2;
        if (mid * mid == n) {
            ans = mid;
        else if (mid * mid < n) {
            ans = mid;
            l = mid + 1;
        else {
            r = mid - 1;
    cout << ans << endl;

int main() {
    return 0;

The output of this code snippet will be 4.

`ans` variable is initialized with the value of `n` before the while loop. The while loop execution is taking place until `l` becomes greater than `r`. Inside the loop, `mid` is calculated as an average integer of `l` and `r`. If the square of `mid` is equal to `n`, `ans` is assigned to `mid`. If the square of `mid` is less than `n`, `ans` is assigned to `mid` and `l` is assinged to `mid+1`. Else, `r` is assigned to `mid-1`. After the loop ends, `ans` is printed on screen which gives us the square root of `n` (i.e `4`).

Time Complexity of Sieve of Eratosthenes for Prime Checking

The time complexity of the Sieve of Eratosthenes for checking if a given number is prime is O(1) because the algorithm uses precomputation to generate a list of prime numbers up to the maximum value, and then checks if the given number is present in that list. The precomputation step takes O(n log(logn)) time complexity, where n is the value up until which primes are to be computed. The space complexity of this algorithm is O(n).

Therefore, the correct option is: O(nlog(logn)) Precomputation, O(1) for check.Missing code detected. Please provide the full code snippet so that we can provide an accurate answer to the question.

Optimizing Binary Search Algorithm


def binary_search(arr, target):
    A function that implements the binary search algorithm
    to search for a target element in a sorted array.

    arr: A sorted list / array of integers.
    target: An integer to be searched in the arr.

    The index of the target element in arr,
    or -1 if it is not found.
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            low = mid + 1
            high = mid - 1
    return -1


This code implements the Binary Search Algorithm, optimized by iteratively reducing the search interval by half. The function `binary_search(arr, target)` takes in two arguments, `arr` which is the sorted array, and `target` which is an integer to be searched in `arr`.

Initially, the function declares variables `low` and `high` which are the index values of the first and last elements of the array, respectively. We then calculate the mid-point value index, `mid`, using the expression `(low + high) // 2`.

Next, we compare the value at `arr[mid]` with the target value. If they are equal, we return the index of the `mid` value. If the `mid` value is less than the target value, we adjust the index interval as `low = mid + 1`, which means that we only need to search the subarray from `mid+1` to the last value, `high`. Similarly, if the `mid` value is greater than the target, we adjust index interval as `high = mid - 1`, which means that we only need to search the subarray from first value, `low` to `mid-1`.

This process of comparing and adjusting index value intervals continues until the target element is found or the search interval is empty, in which case we return -1.

The time complexity of this algorithm is O(log n), as each comparison reduces the search interval by half, ensuring average log₂n comparisons in the worst-case scenario. Thus, the binary search algorithm is an efficient means of searching sorted data for specific values.

Time complexity to insert an element to the front of a Linked List

The time complexity to insert an element to the front of a Linked List (given the head pointer) is O(1). This is because we simply set the next node to the head of the list and then return that node as the new head.

Node* insertToFront(Node* head, int data) {
    Node* newNode = new Node(data);
    newNode->next = head;
    head = newNode;
    return head;

Time Complexity for Inserting Element to the Rear of a LinkedList

The time complexity to insert an element to the rear of a LinkedList, given the head pointer, is O(n).

This is because we need to traverse through the entire LinkedList to reach the end in order to add the new element. Therefore, the time taken for the traversal itself is O(n), making the overall time complexity O(n).

This code has syntax errors and would not compile. The line is missing `<< sum;` part which actually prints the value calculated into console. Assuming it is fixed, the value of "sum" after running the code snippet would be the sum of all the "val" attributes of the LinkedList, which in this case would be 15.

// solve method is called by passing "root", the head of a LinkedList as an argument
void solve(ListNode* root) {
   The LinkedList is defined as:
   root-> val = value of the node
   root-> next = address of next element from the node 
   The List is 1 -> 2 -> 3 -> 4 -> 5
   int sum = 0;
   while(root != NULL) { // only change was made from root -> next to root
       sum += root -> val;
       root = root -> next;
   cout << sum; // prints the sum calculated in the while loop

Operations on LinkedList

A LinkedList data structure can be used to perform various operations:

  1. Implementation of Stacks and Queues
  2. Implementation of Binary Trees
  3. Implementation of Data Structures that can simulate Dynamic Arrays

All of the above operations can be performed using a LinkedList.

Information a Node in a LinkedList must Store

A Node in a LinkedList must store both the address of the next node (if it exists) and the value of the current node. Therefore, the correct answer is (C) Both (A) and (B).

Maximum Number of Children in n-ary Tree

In an n-ary tree, the maximum number of children a node can have is 'n'.

For example, in a binary tree, n is equal to 2, which means that each node can have at most two children. Similarly, in a ternary tree, n would be equal to 3, and so on.

Therefore, Option D "n" is the correct answer.

Worst Case Time Complexity of Accessing an Element in a Binary Search Tree

In a Binary Search Tree (BST), the worst-case time complexity to access an element can be O(n), which occurs when the tree is skewed and has only one branch, resembling a linked list. In such a case, we'd need to visit every node, resulting in O(n) time complexity.

However, in a balanced BST, the worst-case time complexity is O(logn). This happens when the element we're searching for is in the deepest level of the tree, and we need to traverse through the height of the tree, which is logn.

Therefore, the answer is O(n) for the worst-case time complexity to access an element in an unbalanced BST, and O(logn) for a balanced BST.

Postorder Traversal in Binary Tree

The correct representation of Postorder Traversal in a Binary Tree is "Left -> Right -> Root". This means that we first traverse to the left node of the tree, then to the right node, and finally to the root node.

The other options listed are not representative of Postorder Traversal in Binary Tree.

- "Left -> Root -> Right" is the representation for Inorder Traversal - "Right -> Left -> Root" is the representation for Preorder Traversal - "Right -> Root -> Left" is not a valid traversal for a Binary Tree.

By following the correct order of Postorder Traversal, we can ensure that all the nodes in the tree are visited in an organized and efficient manner.

Time Complexity to Find Diameter of a Binary Tree

To find the diameter of a binary tree optimally, we can use a single depth-first search (DFS) which takes the time complexity of O(V + E).

AVL Trees: Properties and Characteristics

In computer science, AVL Trees are self-balancing Binary Search Trees (BSTs). BST is a tree data structure where each node has at most two child nodes, namely left child and right child, and it follows the rule that the left subtree contains only nodes with values less than the parent node, whereas the right subtree contains only nodes with values greater than the parent node.

AVL Tree has some additional characteristics:

  • The difference between the heights of left and right nodes (also known as balance factor) cannot be more than 1.
  • The height of an AVL Tree always remains of the order of O(log n), where n is the number of nodes.
  • AVL Trees are self-balancing which means when an insert or delete operation is performed on the tree, it automatically balances the tree to maintain the optimal height.

Therefore, option (d) is correct - all the above options are applicable for an AVL Tree.

// Sample implementation of AVL Tree in Python
class AVLTree:
  def __init__(self):
    self.root = None

  class Node:
    def __init__(self, key):
      self.key = key
      self.left = None
      self.right = None
      self.height = 1
  # ... Other methods for AVL Tree operations 

The given code snippet calculates the total number of edges in an undirected graph using adjacency list representation.

Determining the Number of Cycles in a Graph

In a graph with n nodes and n edges, the number of cycles present depends on the structure of the graph. However, if the graph is a tree, then there will be no cycles in the graph.

When a single edge is added to a tree, it creates exactly one cycle in the graph. Therefore, if n nodes and n-1 edges form a tree, and we add one more edge to the graph, it will result in exactly one cycle.

So, the answer is exactly 1.

Centroid of a Tree

In a tree, a centroid is a node that when removed splits the tree into smaller trees, each of which has no more than half of the nodes in the original tree. The centroid of a tree always exists and is unique.This code snippet performs a depth-first search (DFS) on a tree represented as a graph, using recursion. It takes in a starting node, a vector of edges (which represents the tree), a vector to keep track of nodes that have been visited (vis), and a vector to store the depth of each node (dp). It sets the current node to visited, and then iterates over all the edges connected to that node, and for each unvisited edge, it sets its depth (dp) to the depth of the current node plus one, and then recursively calls the dfs function on that edge. Ultimately, the dp vector will contain the depth of each node in the tree with respect to the root node.

Shortest Path Algorithms for Weighted Graphs

To find the shortest path from a source node to all other nodes in a weighted graph, we can use various algorithms. Out of the given options, the algorithm that is used for this purpose is Djikstra's Algorithm.

BFS (Breadth First Search) explores the shallowest nodes in a graph and may not necessarily give us the shortest path in a weighted graph. Prim's algorithm and Kruskal's algorithm are used for finding a minimum spanning tree in a weighted graph, not the shortest path from a source node to all other nodes.

Therefore, Djikstra's algorithm remains the optimal choice for finding the shortest path from a source node to all other nodes in a weighted graph.

Optimizing Time Complexity for All Pairs Shortest Paths

To precompute all-pairs shortest paths in a weighted graph, the best time complexity can be achieved using the Floyd-Warshall Algorithm, which has a time complexity of O(n^3). This algorithm computes the shortest path between all pairs of vertices in a graph.

Stacks in Recursive Algorithm

In recursive algorithm, stacks are mainly used for implementation. Recursion is a process in which a function calls itself again and again until a base condition is satisfied. When a function is called, the program needs to store the return address value of the function. This return address value is stored in the stack. Then, the function performs its task and passes control back to the calling function. The return value of the function is also stored in the stack until control is transferred back to the calling function.

def factorial(n):
    if n == 1:
        return 1
        return n * factorial(n-1)

In the above code snippet, recursion is used to calculate the factorial of a number. The function calls itself until the base condition (n==1) is true. During these calls, the return addresses and arguments are stored in the stack.

Technical Interview Guides

Here are guides for technical interviews, categorized from introductory to advanced levels.

View All

Best MCQ

As part of their written examination, numerous tech companies necessitate candidates to complete multiple-choice questions (MCQs) assessing their technical aptitude.

View MCQ's
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.