For the test case:
The judge's expected answer is:
... implying that 1 should be evicted when 4 is inserted into cache, and 2 remains.
However, note that neither 1 nor 2 has been accessed at the time of insertion of 4. The question does not clarify the winner in this case. So either 1 or 2 could be evicted. The test case should be adjusted, or the question should clarify the way to break tie.
I think it says pretty clearly in the description.
"When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted."