Search In Rotated Sorted Array Problem Statement

Given a sorted array of distinct integers that has been rotated clockwise by an unknown amount, you need to search for a specified integer in the array. For each search query, you have to return the index of the element if found; otherwise, return -1.

Input:

The first line contains the size of the array, N.
The second line contains N space-separated integers representing the array elements A[i].
The third line contains the number of queries, Q.
The next Q lines each contain an integer, Q[i], which is the target value to search in the array.

Output:

For each query, output the index of the number if it is present in the array; otherwise, output -1. Each result should be printed on a new line.

Example:

Input:
N = 5
A = [4, 5, 1, 2, 3]
Q = 2
Q[0] = 1
Q[1] = 3
Output:
2
4
Explanation:

For the first query, the element 1 is found at index 2. For the second query, the element 3 is found at index 4.

Constraints:

  • 1 <= N <= 10^6
  • -10^9 <= A[i] <= 10^9
  • 1 <= Q <= 10^5
  • -10^9 <= Q[i] <= 10^9
  • Time complexity should be O(logN) for each query.
Note:

You do not need to handle the printing of the output. Just implement the search function to return the correct result.

AnswerBot
4mo

Implement a search function to find a specified integer in a rotated sorted array with O(logN) time complexity.

  • Implement a binary search algorithm to efficiently search for the target integer.

  • Handle t...read more

Help your peers!
Select
Add answer anonymously...

Paytm Senior Software Engineer interview questions & answers

A Senior Software Engineer was asked 5mo agoQ. What are the best and worst solutions for the coding problem "Longest Substring ...read more
A Senior Software Engineer was asked 9mo agoQ. Given n non-negative integers representing an elevation map where the width of e...read more
A Senior Software Engineer was asked 9mo agoQ. Describe how you would design a system for a chat application.

Popular interview questions of Senior Software Engineer

A Senior Software Engineer was asked 5mo agoQ1. What are the best and worst solutions for the coding problem "Longest Substring ...read more
A Senior Software Engineer was asked 8mo agoQ2. Given n non-negative integers representing an elevation map where the width of e...read more
A Senior Software Engineer was asked 9mo agoQ3. Describe how you would design a system for a chat application.
Paytm Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits