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

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. 2 in this series.

Last Reviewed and Updated on August 18, 2019
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

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.

How many comparisons are performed in the second pass of the selection sort algorithm?

n-2 comparisons are performed in the second pass of the selection sort algorithm.

Write a program in c for linear search.

#include<stdio.h>

int main()
{

    int array[100], search, c, number;

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

    scanf("%d",&number);

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

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

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

    }

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

    scanf("%d",&search);

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

        /* if required element found */
        if ( array[c] == search )
        {

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

            break;

        }

    }

    if ( c == number )
    {

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

    }

    return 0;

}

Advertisement

Binary searches can be done on an array[Yes/No]?

Yes.

Write an algorithm for inserting a node in a Singly-Linked list, if list is empty .

  1. Allocate memory for the new node.
  2. Assign a value to the data field of the new node.
  3. Make the next field of the new node point to NULL.
  4. Make START point to the new node

Write an algorithm for deleting a node between two nodes of the singly-linked list.

The algorithm to delete a node between two nodes of the singly-linked list is as follows:

  1. Locate the node to be deleted. Mark the node to be deleted as current and its predecessor as previous. To locate current and previous, execute the following steps:
    1. Set previous=START.
    2. Set current=START.
    3. Repeat steps d and e until either the node is found or current becomes NULL.
    4. Make previous point to current.
    5. Make current point to the next node in sequence.
  2. Make the next field of previous point to the successor of current.
  3. Release the memory for the node marked as current

What do you mean by Binary Search Tree?

A binary search tree is a binary tree in which the value of the left child of a node is always less than the value of the node, and the value of the right child of a node is always greater than the value of the node.


The root node contains 52. All the nodes in the left subtree of the root have a value less than 52. Similarly, all the nodes in the right sub-tree of the root node have values greater than 52.

What are the basic criteria for selecting appropriate algorithm?

  1. Execution Time.
  2. Storage Space.
  3. Programming Effort.

Write an algorithm for deleting a node from the beginning of the singly-linked list.

The algorithm to delete a node from the beginning of the list is as follows:

  1. Mark the first node in the list as current.
  2. Make START POINT to the next node in its sequence.
  3. Release he memory for the node marked as current.

What do you understand by folding method?

In this method, an address is calculated by breaking the keys into parts and then adding them. Consider the following keys:


4766934,5656975,4685637,3547807,7569664

Keys Dividing the keys Result
4766934 47+6634+4 6744
5656975 56+5697+5 5758
4685637 46+8563+7 8616
3547807 35+4780+7 4822
7569664 75+6966+4 7045

Now, truncate the result up to the digit based on the sizeof the hash table.Suppose the size of the table is 1000. Therfore, the hash address will be from 0 to 999. In the given example the result consists of four digits. Therefore, truncate the first digit of the result to obtain the address, as shown in fig below:

Keys Address
47669334 744
5656975 758
4685637 616
3547807 822
7569664 045

How do you define tree in a data structure?

A tree is a nonlinear data structure that represents a hierarchical relationship among the various data elements, as shown in the following figure.

Each data elements in a tree is called a node. The topmost node of the tree is called a root

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.