Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Search Items by Prefix Query

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

Your question is Search Items by Prefix Query. 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

Given a sorted list of lowercase strings items and a lowercase query string query, return all items that start with query. The result must preserve the original sorted order. Implement the search efficiently without scanning the entire list unless necessary.

Constraints

  • 1 <= len(items) <= 10^5
  • 0 <= len(query) <= 100
  • 0 <= len(items[i]) <= 100
  • items is sorted in non-decreasing lexicographic order
  • All strings contain only lowercase English letters

Function Signature

def search_items(items, query):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output