KatInterview

Profit, Collinearity, and Identity Merging in Three Parts

Arrays & HashingAuto-graded · 28 test cases
MediumAuto-gradedArrays & Hashingarraysmathunion-find

Last updated

Tests one-pass array scanning, integer-arithmetic collinear point detection, and union-find graph component grouping in a single combined function.

Problem Statement

You are building a multi-purpose analytics utility that processes two kinds of input at once: a sequence of integer measurements and a list of name-pair relationships. Implement a single function analyze_sequence that solves three independent sub-problems and returns their results together in a dictionary.


Sub-problem 1 — Best Single Buy-Sell Gain (max_profit)

Given the sequence as a list of integers, find the maximum gain achievable by choosing a buy index i and a sell index j such that i < j. If no positive gain exists (including when the list has fewer than two elements), return 0.


Sub-problem 2 — Earliest Collinear Quadruplet (find_collinear_quad)

Treat each element as a 2D point: index t becomes the x-coordinate and sequence[t] becomes the y-coordinate. Find four distinct indices [i, j, k, l] with i < j < k < l such that all four corresponding points lie exactly on the same straight line.

  • If several such quadruplets exist, return the lexicographically smallest one.
  • If none exists, return None.
  • All collinearity checks must use integer arithmetic (cross-multiplication) to avoid floating-point errors.

Sub-problem 3 — Connected Name Groups (group_aliases)

You are given a list of pairs (a, b) meaning that name a and name b refer to the same entity. Treating these pairs as edges in an undirected graph, find every connected component.

  • Within each component, sort the names lexicographically.
  • Sort the list of components lexicographically by each component's first element.
  • A self-pair ("x", "x") produces a singleton group ["x"].
  • If no pairs are given, return an empty list.

Return Value

Return a single dictionary with exactly these keys:

{
    "max_profit":        int,
    "find_collinear_quad": list[int] | None,
    "group_aliases":     list[list[str]],
}

Constraints

Shared input — sequence:

  • 0 <= len(sequence) <= 10^5 (sub-problem 1)
  • 4 <= len(sequence) <= 2000 (sub-problem 2; the collinear search only runs within this range)
  • -10^9 <= sequence[t] <= 10^9

Sub-problem 1 — max_profit:

  • Values can be negative or very large; assume 64-bit integer arithmetic.
  • Return 0 for any input where no profitable trade is possible.

Sub-problem 2 — find_collinear_quad:

  • Use cross-multiplication for slope comparison: points (x1,y1), (x2,y2), (x3,y3) are collinear when (y2-y1)*(x3-x1) == (y3-y1)*(x2-x1).

Sub-problem 3 — group_aliases (via pairs):

  • 0 <= len(pairs) <= 5 * 10^4
  • Each name consists of 1 to 32 lowercase English letters.

Examples

Example 1

Input: analyze_sequence([10, 7, 5, 8, 11, 9], [])

Output: {'max_profit': 6, 'find_collinear_quad': None, 'group_aliases': []}

Example 2

Input: analyze_sequence([9, 8, 7, 5], [])

Output: {'max_profit': 0, 'find_collinear_quad': None, 'group_aliases': []}

Example 3

Input: analyze_sequence([2, 5, 8, 10, 14, 17], [])

Output: {'max_profit': 15, 'find_collinear_quad': [0, 1, 2, 4], 'group_aliases': []}

Example 4

Input: analyze_sequence([0, 0, 0, 0], [['a', 'a']])

Output: {'max_profit': 0, 'find_collinear_quad': [0, 1, 2, 3], 'group_aliases': [['a']]}

Hints

Hint 1

For the max gain, you only ever need to remember one thing as you scan left to right: the smallest value seen so far. Every element to its right is a candidate sell.

Hint 2

For the collinear quadruplet, fix a base index i and hash the reduced slope from i to every later index j. Use gcd to normalize the slope fraction so integer equality replaces floating-point comparison. The moment a slope bucket hits three entries, you have a candidate quadruplet.

Hint 3

For grouping, model the pairs as graph edges and apply union-find. After processing all pairs, collect every name by its root, sort each bucket internally, then sort the bucket list by its first element.