# Subjective Interview Questions on Linear Search Set 2

## Interview questions for beginners on linear search. Use them for preparing for a job interview. The questions have been answered to the point, and briefly. This is subjective type short answers question and answer set no. 2 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.

## How do you define linear search ?

Linear or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

## What is worst case efficiency of linear search?

The best case efficiency of linear search is O(n).

## What is worst case efficiency of linear search?

The best case efficiency of linear search is O(n).

## C program for linear search using function

```int linear_search(int*, int, int);

int main()
{

int array[100], search, c, n, position;

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);

position = linear_search(array, n, search);

if ( position == -1 )
{

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

}

else
{

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

}

return 0;

}

int linear_search(int *pointer, int n, int find)
{

int c;

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

if ( *(pointer+c) == find )
{

return c;

}

}

return -1;

}

```

## Describe recursive version of linear search

Linear search can also be described as a recursive algorithm:

LinearSearch(value, list)
if the list is empty, return ^;
else
if the first item of the list has the desired value, return its location;
else return LinearSearch(value, remainder of the list)

## Linear search is special case of which search.

Linear search is special case of brute-force search.

## What are the various applications of linear search?

Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list. When many values have to be searched in the same list, it often pays to pre-process the latter in order to use a faster method.

## 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;

}

```

## How do you define linear search ?

Linear or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

## Write an algorithm to search for an employee ID in an array(Hint: use linear search)

1. Read the employee ID to be searched
2. Set i=0
3. Repeat step d until i>n or arr[i]=employee ID
4. Increment i by 1