PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-processing and algorithmic problem-solving skills, focusing on identifying substrings unique to each word while handling tie-breaking rules like starting index and lexicographic order and considering time/space trade-offs.

  • medium
  • Affirm
  • Coding & Algorithms
  • Software Engineer

Find shortest unique substring per word

Company: Affirm

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Shortest unique substring for each word You are given an array of **distinct** strings `words`. For each word `w` in `words`, find a **non-empty contiguous substring** of `w` that **does not appear as a substring in any other word** in the array. Return, for every word, the **shortest** such substring. ### Tie-breaking If a word has multiple valid substrings with the same minimal length: 1. Choose the one with the **smallest starting index** in the word. 2. If there is still a tie, choose the **lexicographically smallest** substring. You may assume that for every word, at least one such substring exists. ### Example **Input**: ```text ['cheapair', 'cheapoair', 'peloton', 'pelican'] ``` **Output**: ```text { 'cheapair': 'pa', 'cheapoair': 'po', 'peloton': 't', 'pelican': 'li' } ``` ### Constraints (typical interview ranges) - `1 <= len(words) <= 2e3` - `1 <= len(words[i]) <= 2e3` - Total characters across all words `<= 2e4` - Lowercase English letters (you may generalize if desired)

Quick Answer: This question evaluates string-processing and algorithmic problem-solving skills, focusing on identifying substrings unique to each word while handling tie-breaking rules like starting index and lexicographic order and considering time/space trade-offs.

For each word, return its shortest substring absent from all other words. Ties use lexicographic order; return an empty string if none exists.

Constraints

  • Inputs are provided as Python literals compatible with the function signature.
  • Return a deterministic value exactly matching the requested output.

Examples

Input: (['cab', 'ad', 'bad', 'c'],)

Expected Output: ['ab', '', 'ba', '']

Explanation: Includes a word with no unique substring.

Input: (['apple', 'apply', 'ape'],)

Expected Output: ['le', 'y', 'pe']

Explanation: Tie-breaking.

Input: (['same', 'same'],)

Expected Output: ['', '']

Explanation: Duplicates.

Hints

  1. Start with a direct data structure representation.
  2. Handle edge cases before the main loop.
Last updated: Jun 27, 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
  • AI Coding 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

  • Determine Redeemable Promotion Offers - Affirm (medium)
  • Compute Available Offers per User - Affirm (easy)
  • Aggregate loans and match repayments - Affirm (medium)
  • Implement a timestamped map - Affirm (medium)
  • Detect fraud events and extract PII - Affirm (medium)