PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Affirm

Find shortest unique substring per word

Last updated: Mar 29, 2026

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.

Related Interview 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)
Affirm logo
Affirm
Oct 27, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
0
Loading...

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:

['cheapair', 'cheapoair', 'peloton', 'pelican']

Output:

{
  '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)

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Affirm•More Software Engineer•Affirm Software Engineer•Affirm Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,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.