Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse String and Binary Search

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reverse String and Binary Search. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

AVEVA engineering tools may need to display a reversed identifier while efficiently locating a value in an ordered collection. Implement both operations in a single function.

Given a string text, a sorted list of distinct integers values, and an integer target:

  1. Return text reversed.
  2. Use binary search to return the zero-based index of target in values, or -1 if the target is absent.

The list values is sorted in ascending order. Do not use Python's list.index(), in, bisect, or any library search function for the lookup. The string reversal may use a loop or Python slicing.

Formal Specification

  • Input: text, a string; values, a sorted list of distinct integers; and target, an integer.
  • Output: a two-element list [reversed_text, index], where reversed_text is a string and index is the target's position or -1.

Constraints

  • 0 <= len(text) <= 10^4
  • 0 <= len(values) <= 10^5
  • -10^9 <= values[i], target <= 10^9
  • values is sorted in strictly ascending order

Function Signature

def reverse_and_search(text, values, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output