Find a target in a sorted array of strings interspersed with empty strings

Quick Overview

Search a sorted array of strings with empty strings scattered through it and return the smallest index of a target, or -1 if it is missing. It tests adapting binary search when some probes carry no ordering information, handling duplicates, and reasoning about the worst-case running time.

Find a target in a sorted array of strings interspersed with empty strings

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array of strings `strings` that may contain **empty strings** scattered throughout, and a non-empty `target` string. Return the **index** of `target` in the array, or `-1` if it does not appear. The array is sorted in **non-decreasing lexicographic order** when the empty strings are ignored. Solve it with a binary search that handles the empty strings during the search. ### Function Signature ```python def search_string(strings: list[str], target: str) -> int: ``` ### Rules - Empty strings can sit anywhere and carry no ordering information. The non-empty strings, read from left to right, are in non-decreasing lexicographic order. - Strings compare in ordinary lexicographic order, the same order Python's `<` uses on lowercase strings. - If `target` appears more than once, return the smallest index at which it appears. - If `target` does not appear, return `-1`. ### Constraints - `1 <= len(strings) <= 10^5` - `0 <= len(strings[i]) <= 100` - `1 <= len(target) <= 100` - `strings` is sorted in non-decreasing lexicographic order, ignoring empty strings - `target` is not an empty string - All strings consist of lowercase English letters ### Examples ```text Input: strings = ["apple", "", "", "banana", "", "cherry", "date", "", "fig"], target = "cherry" Output: 5 ``` Without the empty strings the array reads `["apple", "banana", "cherry", "date", "fig"]`, and `"cherry"` sits at index 5 of the original array. ```text Input: strings = ["", "", "abc", "", "def", "", "", "ghi", "jkl", ""], target = "xyz" Output: -1 ``` The non-empty strings are `"abc"`, `"def"`, `"ghi"` and `"jkl"`, and none of them is `"xyz"`. ```text Input: strings = ["", "bee", "", "", "bee", "bee", "cat", ""], target = "bee" Output: 1 ``` `"bee"` appears at indices 1, 4 and 5. The smallest of those is 1.

Overview: Search a sorted array of strings with empty strings scattered through it and return the smallest index of a target, or -1 if it is missing. It tests adapting binary search when some probes carry no ordering information, handling duplicates, and reasoning about the worst-case running time.

Google logo
Google
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of strings strings that may contain empty strings scattered throughout, and a non-empty target string. Return the index of target in the array, or -1 if it does not appear.

The array is sorted in non-decreasing lexicographic order when the empty strings are ignored. Solve it with a binary search that handles the empty strings during the search.

Function Signature

def search_string(strings: list[str], target: str) -> int:

Rules

  • Empty strings can sit anywhere and carry no ordering information. The non-empty strings, read from left to right, are in non-decreasing lexicographic order.
  • Strings compare in ordinary lexicographic order, the same order Python's < uses on lowercase strings.
  • If target appears more than once, return the smallest index at which it appears.
  • If target does not appear, return -1 .

Constraints

  • 1 <= len(strings) <= 10^5
  • 0 <= len(strings[i]) <= 100
  • 1 <= len(target) <= 100
  • strings is sorted in non-decreasing lexicographic order, ignoring empty strings
  • target is not an empty string
  • All strings consist of lowercase English letters

Examples

Input: strings = ["apple", "", "", "banana", "", "cherry", "date", "", "fig"], target = "cherry"
Output: 5

Without the empty strings the array reads ["apple", "banana", "cherry", "date", "fig"], and "cherry" sits at index 5 of the original array.

Input: strings = ["", "", "abc", "", "def", "", "", "ghi", "jkl", ""], target = "xyz"
Output: -1

The non-empty strings are "abc", "def", "ghi" and "jkl", and none of them is "xyz".

Input: strings = ["", "bee", "", "", "bee", "bee", "cat", ""], target = "bee"
Output: 1

"bee" appears at indices 1, 4 and 5. The smallest of those is 1.