PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string manipulation and array processing, focusing on identifying common prefixes, handling edge cases such as empty inputs, and reasoning about algorithmic time and space complexity.

  • easy
  • Apple
  • Coding & Algorithms
  • Software Engineer

Find Common Prefix Across Strings

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given an array of strings, return the longest string prefix that is shared by every string in the array. A prefix must start at index 0 of each string. If there is no common prefix, return an empty string. You should be prepared to describe and implement two different approaches. Examples: - Input: `["flower", "flow", "flight"]` Output: `"fl"` - Input: `["dog", "racecar", "car"]` Output: `""` - Input: `["interview"]` Output: `"interview"` Clarifications: - If the input array is empty, return an empty string. - Strings may be empty. - Assume all strings contain standard ASCII characters unless stated otherwise.

Quick Answer: This question evaluates proficiency in string manipulation and array processing, focusing on identifying common prefixes, handling edge cases such as empty inputs, and reasoning about algorithmic time and space complexity.

Given an array of strings, return the longest prefix shared by every string in the array. A prefix must start at index 0 of each string. If there is no common prefix, return an empty string. You should also be ready to discuss two possible approaches, such as scanning character-by-character or using sorting to compare the most different strings.

Constraints

  • 0 <= len(strs) <= 10000
  • 0 <= len(strs[i]) <= 200
  • All strings contain standard ASCII characters
  • The prefix must begin at index 0 in every string

Examples

Input: (["flower", "flow", "flight"],)

Expected Output: "fl"

Explanation: All three strings start with "fl", but they differ at the next character.

Input: (["dog", "racecar", "car"],)

Expected Output: ""

Explanation: The first characters already differ, so there is no shared prefix.

Input: (["interview"],)

Expected Output: "interview"

Explanation: With only one string, that entire string is the common prefix.

Input: ([],)

Expected Output: ""

Explanation: An empty input array has no common prefix.

Input: (["", "abc", "ab"],)

Expected Output: ""

Explanation: Because one string is empty, the longest common prefix must be empty.

Input: (["prefix", "prefix", "prefixes"],)

Expected Output: "prefix"

Explanation: The shortest full shared starting sequence across all strings is "prefix".

Hints

  1. The common prefix can never be longer than the shortest string in the array.
  2. Try comparing characters at the same index across all strings. As an alternative idea, sort the strings and compare only the first and last.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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

  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)