KatInterview

Runner-Up Label by Frequency Tier in a Flat Record List

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinghash-mapcountingfrequency

Last updated

Count labels in every third slot of a flat list, find the second-highest distinct count, and break ties by earliest appearance.

Runner-Up Label by Frequency Tier

A flat list of strings stores records back to back, three items per record:

[id1, name1, label1, id2, name2, label2, ...]

Each record is the triple (id, name, label). Only the labels matter for this task. They sit at indices 2, 5, 8, ....

Task

Return the label in the second-highest frequency tier.

  • Count how many times each distinct label occurs.
  • Collect the distinct count values and rank them from highest to lowest. The runner-up tier is the one with the second-highest distinct count.
  • Several labels can share a tier. If several labels have the runner-up count, return the one that appears earliest in the list as a label, meaning the smallest index among the label positions. Ids and names are never considered for this tie-break.
  • If there is no second tier, return the string "notag". This happens when every label has the same count, when there is only one distinct label, or when the list is empty.

For example, with counts x=2, y=2, z=1, the top tier is count 2 (x and y) and the runner-up tier is count 1, so the answer is z. Tied labels at the top do not count as the "second" label.

Input

  • entries: a list of strings whose length is a multiple of 3, laid out as described above.

Output

  • A string: the runner-up label, or "notag" if none exists.

Constraints

  • len(entries) is a multiple of 3 (it may be 0).
  • Each element is a string.
  • An id or name may equal some label string. Such occurrences are not counted and are not used for tie-breaking.
  • The expected running time is O(n), where n is len(entries).

Examples

Example 1

Input: runner_up_tag(['1', 'A', 'x', '2', 'B', 'y', '3', 'C', 'y', '4', 'D', 'x', '5', 'E', 'z'])

Output: 'z'

Example 2

Input: runner_up_tag(['1', 'A', 'red', '2', 'B', 'blue', '3', 'C', 'red', '4', 'D', 'green', '5', 'E', 'blue', '6', 'F', 'green'])

Output: 'notag'

Example 3

Input: runner_up_tag(['1', 'A', 'x', '2', 'B', 'x', '3', 'C', 'x'])

Output: 'notag'

Example 4

Input: runner_up_tag(['1', 'A', 'a', '2', 'B', 'b', '3', 'C', 'b', '4', 'D', 'c', '5', 'E', 'c', '6', 'F', 'c', '7', 'G', 'a', '8', 'H', 'd'])

Output: 'a'

Hints

Hint 1

Only every third element, starting at index 2, is a label. Step through the list with a stride of 3 instead of grouping records.

Hint 2

Rank distinct count values, not labels. Several labels may share the top count, and they all belong to the top tier. Find the largest count that is strictly below the maximum.

Hint 3

Python dicts remember insertion order, so a count dictionary also records the order in which labels first appeared. Scan it once more and return the first label whose count equals the runner-up count.