Compare two concatenated word lists and find the last price at or below a target

Quick Overview

Given two lists of string fragments, decide whether they join into the same string, then return the index of the last price at or below a target in a sorted list. Tests linear-time string comparison, binary search on sorted data with repeated values, and off-by-one handling at the edges.

Compare two concatenated word lists and find the last price at or below a target

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two lists of strings, `words1` and `words2`, a list of integers `prices` sorted in non-decreasing order, and an integer `target`. First, decide whether `words1` and `words2` represent the same concatenated string: joining all strings in `words1` in order must produce the same result as joining all strings in `words2` in order. Make this comparison efficient. It should not use a nested loop with a character-by-character comparison. Second, find the value in `prices` that is equal to `target` or, when no value equals it, the next lowest value below `target`, and return that value's index. If `target` is smaller than every value in `prices`, the index is `-1`. Return a list of two elements: a boolean saying whether the strings match, and the index from the price lookup. ### Function Signature ```python def price_is_right(words1: list[str], words2: list[str], prices: list[int], target: int) -> list: ``` ### Rules - The return value is `[match, index]`, where `match` is a Python `bool` and `index` is an `int`. - `index` is 0-based and points at the largest value in `prices` that is less than or equal to `target`. - `prices` may contain repeated values. When the value found appears more than once, return the largest index holding it (its last occurrence). - The index is `-1` when `target < prices[0]`. ### Constraints - `1 <= len(words1), len(words2) <= 10^3` - `1 <= len(words1[i]), len(words2[j]) <= 10^3` - `1 <= len(prices) <= 10^5` - `1 <= prices[i] <= 10^9` - `prices` is sorted in non-decreasing order - `1 <= target <= 10^9` ### Examples ```text Input: words1 = ["ab", "c"], words2 = ["a", "bc"], prices = [100, 200, 300, 400, 500], target = 350 Output: [True, 2] ``` Both lists join to `"abc"`, so the strings match. The largest price at or below 350 is 300, at index 2. ```text Input: words1 = ["hello"], words2 = ["hel", "lo"], prices = [10, 20, 30, 40, 50], target = 5 Output: [True, -1] ``` Both lists join to `"hello"`. The target 5 is smaller than every price, so the index is -1. ```text Input: words1 = ["abc", "d"], words2 = ["ab", "cd", "e"], prices = [10, 20, 20, 20, 30], target = 20 Output: [False, 3] ``` The joins are `"abcd"` and `"abcde"`, which differ. The value 20 equals the target and sits at indices 1, 2 and 3, so its last occurrence, index 3, is returned.

Overview: Given two lists of string fragments, decide whether they join into the same string, then return the index of the last price at or below a target in a sorted list. Tests linear-time string comparison, binary search on sorted data with repeated values, and off-by-one handling at the edges.

Spacex logo
Spacex
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two lists of strings, words1 and words2, a list of integers prices sorted in non-decreasing order, and an integer target.

First, decide whether words1 and words2 represent the same concatenated string: joining all strings in words1 in order must produce the same result as joining all strings in words2 in order. Make this comparison efficient. It should not use a nested loop with a character-by-character comparison.

Second, find the value in prices that is equal to target or, when no value equals it, the next lowest value below target, and return that value's index. If target is smaller than every value in prices, the index is -1.

Return a list of two elements: a boolean saying whether the strings match, and the index from the price lookup.

Function Signature

def price_is_right(words1: list[str], words2: list[str], prices: list[int], target: int) -> list:

Rules

  • The return value is [match, index] , where match is a Python bool and index is an int .
  • index is 0-based and points at the largest value in prices that is less than or equal to target .
  • prices may contain repeated values. When the value found appears more than once, return the largest index holding it (its last occurrence).
  • The index is -1 when target < prices[0] .

Constraints

  • 1 <= len(words1), len(words2) <= 10^3
  • 1 <= len(words1[i]), len(words2[j]) <= 10^3
  • 1 <= len(prices) <= 10^5
  • 1 <= prices[i] <= 10^9
  • prices is sorted in non-decreasing order
  • 1 <= target <= 10^9

Examples

Input: words1 = ["ab", "c"], words2 = ["a", "bc"], prices = [100, 200, 300, 400, 500], target = 350
Output: [True, 2]

Both lists join to "abc", so the strings match. The largest price at or below 350 is 300, at index 2.

Input: words1 = ["hello"], words2 = ["hel", "lo"], prices = [10, 20, 30, 40, 50], target = 5
Output: [True, -1]

Both lists join to "hello". The target 5 is smaller than every price, so the index is -1.

Input: words1 = ["abc", "d"], words2 = ["ab", "cd", "e"], prices = [10, 20, 20, 20, 30], target = 20
Output: [False, 3]

The joins are "abcd" and "abcde", which differ. The value 20 equals the target and sits at indices 1, 2 and 3, so its last occurrence, index 3, is returned.