KatInterview

Find Array Peak Index and Form Unique Word Abbreviation List

Heaps, Sorting & SearchAuto-graded · 23 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary-searchstringshash-map

Last updated

Locate a local peak index in an array via binary search and generate minimal unambiguous string abbreviations for a list of words.

Write a function that solves two combined sub-problems and returns their outputs in a single list format.

Sub-problem 1: Binary Search Peak Finding

Given an array of integers a of length $N \ge 1$:

  • Assume virtual values out of bounds are $-\infty$: $a[-1] = a[N] = -\infty$.
  • An index $i$ is a peak if $a[i] > a[i-1]$ and $a[i] > a[i+1]$.
  • Locate a peak index using binary search by maintaining standard low/high boundaries lo = 0, hi = N - 1, mid = (lo + hi) // 2, moving lo = mid + 1 if $a[\text{mid}] < a[\text{mid}+1]$ and hi = mid otherwise.

Sub-problem 2: Unique String Abbreviations

Given a list of distinct strings k (words composed of lowercase letters):

  • Words of length $\le 3$ cannot be abbreviated to a shorter form, so they always remain unchanged as the full word.
  • For a word of length $> 3$, an abbreviation with prefix length $p$ ($1 \le p < \text{len}(w) - 1$) takes the form: $$\text{word}[:p] + \text{str}(\text{len}(\text{word}) - p - 1) + \text{word}[-1]$$
  • If $p \ge \text{len}(\text{word}) - 2$, the abbreviation length would not be strictly shorter than the word length, so the original full word is used instead.
  • Initially, set prefix length $p = 1$ for all words of length $> 3$.
  • In each step, form the current candidate string (either abbreviated form or full word) for every word in k.
  • Group the words by their candidate string.
  • Identify all groups that contain 2 or more words (collisions).
  • For every word belonging to any colliding group, increment its prefix length $p$ by 1 (up to $\text{len}(\text{word})$ if $p$ reaches $\text{len}(\text{word}) - 2$, effectively reverting it to its full word). Words not in a collision keep their current $p$.
  • Repeat the process iteratively until all candidate strings across the list are mutually distinct (i.e. no collision groups exist).

Return Format

Return a list containing two elements:

  1. [peak_index]: A list with the single peak index found in sub-problem 1.
  2. abbreviations: A list of strings containing the final unique abbreviation or full word for each word in k in the original input order.

Constraints

  • $1 \le \text{len}(a) \le 10^5$
  • $-2^{31} \le a[i] \le 2^{31} - 1$
  • $0 \le \text{len}(k) \le 1000$
  • $1 \le \text{len}(w) \le 100$ for each word $w \in k$
  • All words in $k$ consist of lowercase English letters and are distinct.

Examples

Example 1

Input: find_peak_and_unique_abbreviations([1, 2, 3, 1], ['international', 'interview', 'internet'])

Output: [[2], ['i11l', 'i7w', 'i6t']]

Example 2

Input: find_peak_and_unique_abbreviations([1], [])

Output: [[0], []]

Example 3

Input: find_peak_and_unique_abbreviations([3, 2, 1], ['apple', 'apply'])

Output: [[0], ['a3e', 'a3y']]

Example 4

Input: find_peak_and_unique_abbreviations([1, 3, 2, 4, 1], ['abcdef', 'abcxef', 'abcyef'])

Output: [[3], ['abcdef', 'abcxef', 'abcyef']]

Hints

Hint 1

For peak finding, compare a[mid] to a[mid + 1]. If a[mid] < a[mid + 1], a peak must lie to the right; otherwise, it lies at mid or to the left.

Hint 2

Start each word of length > 3 with prefix length p = 1 and generate initial candidate abbreviations.

Hint 3

Group words by their current candidate string. Any candidate string with multiple words constitutes a collision. Increment p for all words in colliding groups and repeat until no collisions remain.