Questions on various Topics of Data Structures(Randomly Mixed) Set 9

Interview questions on binary search, bubble sort, linear search, linked lists, selection sort and trees in data structures. These questions have been arranged in random order. The questions are meant for a fresher preparing for a job interview. Please inform us if you find any mistakes. This is subjective type short answers question and answer set no. 9 in this series.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Interview and Exam Questions and Answers

This set contains a list of commonly asked questions. They are short interview questions aimed at freshers interview, campus placement drives, and also for job interviews. You can use these to have a quick grasp and brush-up of your fundamentals. These questions can be viewed on a mobile phone also because this website is built on responsive web design.

Bubble sort in C#

using System;

class AscendingBubbleSort
{

    public static void Main()
    {

        int i = 0,j = 0,t = 0;

        int []c=new int[20];

        for(i=0;i<20;i++)
        {

            Console.WriteLine("Enter Value p[{0}]:", i);

            c[i] = int.Parse(Console.ReadLine());

        }

        // Sorting: Bubble Sort
        for(i=0;i<20;i++)
        {

            for(j=i+1;j<20;j++)
            {

                if(c[i]>c[j])
                {

                    Console.WriteLine(
                    "c[{0}]={1}, c[{2}]={3}",
                    i, c[i], j, c[j]);

                    t=c[i];

                    c[i]=c[j];

                    c[j]=t;

                }

            }

        }

        Console.WriteLine("Here comes the sorted array:");

        // Print the contents of the sorted array
        for(i=0;i<20;i++)
        {

            Console.WriteLine ("c[{0}]={1}", i, c[i]);

        }

    }

}


State one advantage of a Huffman tree.

A Huffman tree helps assign variable length codes to the characters in a message. It also ensures that the character codes have the prefix property.

How do you define hashing?

Hashing is the method of finding and retrieving information associated with a unique identifying key.

Write a program in c for Linear search for multiple occurrences

In the code below we will print all the locations at which required element is found and also the number of times it occur in the list.
#include<stdio.h>

int main()
{

    int array[100], search, c, n, count = 0;

    printf("Enter the number of elements in array\n");

    scanf("%d",&n);

    printf("Enter %d numbers\n", n);

    for ( c = 0 ; c < n ; c++ )
    {

        scanf("%d",&array[c]);

    }

    printf("Enter the number to search\n");

    scanf("%d",&search);

    for ( c = 0 ; c < n ; c++ )
    {

        if ( array[c] == search )
        {

            printf("%d is present at location %d.\n", search, c+1);

            count++;

        }

    }

    if ( count == 0 )
    {

        printf("%d is not present in array.\n", search);

    }

    else
    {

        printf("%d is present %d times in array.\n", search, count);

    }

    return 0;

}


Write an algorithm for inserting a node in a circular linked list when the list is empty.

If the list is empty, the following algorithm is used to insert a node in the linked list.

  1. Allocate a memory fo new node.

  2. Assign a value to the data field of the new node.
  3. Make LAST point to the new node.
  4. Make the next field of new node point to new node

What are different types of linked list?

Singly-linked list, Doubly-linked list and Circular linked list

How is Selection Sort analyzed?

Selection sort is not difficult to analyze compared to other sorting algorithms since none of the loops depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takes n - 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n - 1 elements and so on, for comparisons. Each of these scans requires one swap for (n-1) elements.

What would happen if bubble sort didn't keep track of the number of swaps made on each pass through the list?

The algorithm wouldn't know when to terminate as it would have no way of knowing when the list was in sorted order.

Selection sort the following array. Show the array after each swap that takes place.
{ 30, 60, 20, 50, 40, 10 }


  1. 30 60 20 50 40 10
  2. 10  60 20 50 40 30 
  3. 10 20  60  50 40 30
  4. 10 20 30  50 40 60 
  5. 10 20 30 40  50  60
  6. 10 20 30 40 50  60 (self-swap)
  7. 10 20 30 40 50 60  (self-swap)

Consider the following tree and answer the questions that follow.


  1. What is the depth of the tree?
  2. Which nodes are the children of node B?
  3. Which node is the parent of node F?
  4. What is the level of node E
  5. Which nodes are the siblings of node H?
  6. Which nodes are the siblings of node D?
  7. Which nodes are the leaf nodes?
Answers:
  1. 4
  2. D and E
  3. C
  4. 2
  5. H does not have any siblings
  6. The only sibling of D is E
  7. F,G,H and I

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.