Find the second most frequent tag
Company: SoFi
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to perform frequency analysis and stable tie-breaking on a linear sequence, exercising skills in hashing, counting, and handling edge cases such as equal frequencies.
Constraints
- 0 <= len(data) <= 3 * 10^5
- len(data) % 3 == 0
- All elements of data are strings
- Expected time complexity: O(n), where n is the length of data
Examples
Input: (['1','A','x','2','B','y','3','C','y','4','D','x','5','E','z'],)
Expected Output: 'z'
Explanation: The tag counts are x=2, y=2, z=1. The distinct frequencies are 2 and 1, so the second most frequent count is 1. Therefore the answer is 'z'.
Input: (['1','A','red','2','B','blue','3','C','red','4','D','blue'],)
Expected Output: 'notag'
Explanation: The counts are red=2 and blue=2. All tags have the same frequency, so there is no second most frequent tag.
Input: (['1','N1','b','2','N2','a','3','N3','c','4','N4','a','5','N5','b','6','N6','a','7','N7','c'],)
Expected Output: 'b'
Explanation: The counts are a=3, b=2, c=2. The second most frequent count is 2, shared by b and c. Tag b appears first earlier in the input, so return 'b'.
Input: ([],)
Expected Output: 'notag'
Explanation: There are no records, so there cannot be a second most frequent tag.
Input: (['1','N1','a','2','N2','b','3','N3','c','4','N4','d','5','N5','a','6','N6','b','7','N7','a','8','N8','b','9','N9','c'],)
Expected Output: 'c'
Explanation: The counts are a=3, b=3, c=2, d=1. The highest frequency is 3, and the second highest distinct frequency is 2, so the answer is 'c'.
Hints
- You only need to inspect elements at indices 2, 5, 8, ... because those are the tags.
- Use one hash map for counts and another for each tag's first position, then find the top two distinct frequencies without sorting all tags.