PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string processing and prefix-matching within arrays, testing the ability to identify the first occurrence and handle cases such as empty inputs or no matches.

  • medium
  • Pinterest
  • Coding & Algorithms
  • Software Engineer

Find First Prefix-Matching Word

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an array of strings `words` and a prefix string `prefix`, return the smallest index `i` such that `words[i]` starts with `prefix`. If no word matches, return `-1`. Example: - `words = ["a", "apple", "appz", "b"]` - `prefix = "ap"` - Output: `1` Explanation: `"apple"` is the first word in the array that starts with `"ap"`.

Quick Answer: This question evaluates proficiency in string processing and prefix-matching within arrays, testing the ability to identify the first occurrence and handle cases such as empty inputs or no matches.

Given an array of strings `words` and a string `prefix`, return the smallest index `i` such that `words[i]` starts with `prefix`. If no such word exists, return `-1`. If multiple words match, you must return the earliest one in the array.

Constraints

  • 0 <= len(words) <= 100000
  • 1 <= len(words[i]) <= 100
  • 1 <= len(prefix) <= 100
  • All strings may contain lowercase English letters

Examples

Input: (["a", "apple", "appz", "b"], "ap")

Expected Output: 1

Explanation: "apple" is the first word that starts with "ap".

Input: (["hello", "world"], "wor")

Expected Output: 1

Explanation: "world" starts with "wor", and it appears at index 1.

Input: (["dog", "cat", "fish"], "bir")

Expected Output: -1

Explanation: No word in the array starts with "bir".

Input: ([], "a")

Expected Output: -1

Explanation: The array is empty, so there cannot be any matching word.

Input: (["solo"], "solo")

Expected Output: 0

Explanation: The only word exactly matches the prefix, so the answer is index 0.

Hints

  1. You only need the first matching word, so scan the array from left to right.
  2. Use a direct prefix check on each word instead of comparing entire strings.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)