Katkat

Find the second most frequent tag

SoFiSoftware Engineer · Technical Screen

Last updated

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.

You are given a list of strings in the following repeating order:

[id1, name1, tag1, id2, name2, tag2, ...]

So every 3 consecutive elements describe one record: (id, name, tag).

Return the tag that is the second most frequent among all records’ tags.

Tie/edge-case rules:

  1. If there is no “second most frequent” tag because all tags have the same count (i.e., there is only one distinct frequency), return "notag".
  2. If there are multiple tags tied for second most frequent, return the one whose first appearance in the input list occurs earliest.

Constraints/expectations:

  • Run time should be O(n).
  • You may assume the input length is a multiple of 3.

Example:

Input: ["1","A","x","2","B","y","3","C","y","4","D","x","5","E","z"] Counts: x=2, y=2, z=1 → second most frequent is z → return "z".

Write a function to compute the required tag.