Runner-Up Label by Frequency Tier in a Flat Record List
Arrays & HashingAuto-graded · 19 test casesLast 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Step through the list from index 2 in strides of 3, so only labels are visited. Count each label in a dictionary. Because Python dictionaries keep insertion order, the keys end up ordered by each label's first appearance as a label.
- Compute the highest count,
top. - Compute the runner-up count: the largest count that is strictly smaller than
top. If no such count exists, return"notag". - Walk the dictionary in insertion order and return the first label whose count equals the runner-up count.
Why it is correct
- Every record has exactly one label, at index
3k + 2. Stepping by 3 from index 2 counts each label once and ignores ids and names. - The runner-up count is the maximum over counts strictly below
top. That is the second-highest distinct frequency. Labels tied for the top are all excluded, so for countsx=2, y=2, z=1the answer isz, noty. - If no count is below
top, all labels share one count (or there are no labels), which is exactly the"notag"case. - A label enters the dictionary the first time it is seen. Scanning in insertion order therefore finds the earliest-appearing label among those tied at the runner-up count.
Complexity
- Time: O(n). One pass counts the labels, and two passes over the distinct labels find the runner-up count and the answer.
- Space: O(k), where k is the number of distinct labels.
Reference solution
from typing import List
def runner_up_tag(entries: List[str]) -> str:
counts = {} # insertion order == order of first appearance as a label
for i in range(2, len(entries), 3):
label = entries[i]
counts[label] = counts.get(label, 0) + 1
if not counts:
return "notag"
top = max(counts.values())
runner_up = 0
for c in counts.values():
if c < top and c > runner_up:
runner_up = c
if runner_up == 0:
return "notag"
for label, c in counts.items():
if c == runner_up:
return label
return "notag"