an HCL GUVI product

Searching - DSA Interview Questions

Searching algorithms are fundamental in data structures and algorithms (DSA), playing a crucial role in optimizing data retrieval. Mastering these techniques will help you solve complex coding problems efficiently and improve your problem-solving skills for technical interviews.

Practice Searching DSA Coding Problems with Solutions

Learning Objectives:

Understand various searching algorithms, including linear and binary search, and their time complexities. Learn how to optimize search operations using advanced techniques like interpolation search, exponential search, and ternary search.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Linear Search

Required Input:

[10, 20, 5, 7, 15] 
7

Expected Output:

3

Code In Python

def linear_search(arr, target): # Write your logic here pass # Prefilled input arr = [10, 20, 5, 7, 15] target = 7 print(linear_search(arr, target))

Run Code?

Click Run Button to view compiled output

2. Binary Search

Required Input:

[2, 4, 6, 8, 10, 12] 
6

Expected Output:

2

Code In Python

def binary_search(arr, target): # Write your logic here pass # Prefilled input arr = [2, 4, 6, 8, 10, 12] target = 6 print(binary_search(arr, target))

Run Code?

Click Run Button to view compiled output

3. First and Last Position

Required Input:

[5, 7, 7, 8, 8, 10] 
8

Expected Output:

[3, 4]

Code In Python

def find_first_and_last(arr, target): # Write your logic here pass # Prefilled input arr = [5, 7, 7, 8, 8, 10] target = 8 print(find_first_and_last(arr, target))

Run Code?

Click Run Button to view compiled output

4. Peak Element (Unimodal)

Required Input:

[1, 3, 8, 12, 4, 2]

Expected Output:

12

Code In Python

def find_peak_element(arr): # Write your logic here pass # Prefilled input arr = [1, 3, 8, 12, 4, 2] print(find_peak_element(arr))

Run Code?

Click Run Button to view compiled output

5. Square Root (Integer)

Required Input:

x = 25

Expected Output:

5

Code In Python

def square_root(x): # Write your logic here pass # Prefilled input x = 25 print(square_root(x))

Run Code?

Click Run Button to view compiled output

6. Insert Position

Required Input:

[1, 3, 5, 6] 
2

Expected Output:

1

Code In Python

def search_insert_position(arr, target): # Write your logic here pass # Prefilled input arr = [1, 3, 5, 6] target = 2 print(search_insert_position(arr, target))

Run Code?

Click Run Button to view compiled output

7. Min in Rotated Array

Required Input:

[4, 5, 6, 7, 0, 1, 2]

Expected Output:

0

Code In Python

def find_min_rotated(arr): # Write your logic here pass # Prefilled input arr = [4, 5, 6, 7, 0, 1, 2] print(find_min_rotated(arr))

Run Code?

Click Run Button to view compiled output

8. Search in Matrix

Required Input:

    [1, 4, 7, 11],
    [2, 5, 8, 12],
    [3, 6, 9, 16],
    [10, 13, 14, 17]
5

Expected Output:

True

Code In Python

def search_matrix(matrix, target): # Write your logic here pass # Prefilled input matrix = [ [1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17] ] target = 5 print(search_matrix(matrix, target))

Run Code?

Click Run Button to view compiled output

9. Single Non-Repeating

Required Input:

[1,1,2,2,3,3,4,8,8]

Expected Output:

4

Code In Python

def single_non_repeating(arr): # Write your logic here pass # Prefilled input arr = [1, 1, 2, 2, 3, 3, 4, 8, 8] print(single_non_repeating(arr))

Run Code?

Click Run Button to view compiled output

10. Is Sorted & Rotated

Required Input:

[3, 4, 5, 1, 2]

Expected Output:

True

Code In Python

def is_sorted_and_rotated(arr): # Write your logic here pass # Prefilled input arr = [3, 4, 5, 1, 2] print(is_sorted_and_rotated(arr))

Run Code?

Click Run Button to view compiled output

1 of 3