FastPrepCache Access Statistics
Problems/Cache Access Statistics

Cache Access Statistics

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: A Goldman Sachs onsite report asks for cache logic backed by ConcurrentHashMap and for hit, miss, and per-key frequency counts. The source omits operations, return encoding, update semantics, and eviction behavior; this no-eviction chronological batch preserves the observable statistics and is an approximately 82% match.

Process operations against an initially empty string cache:

  • PUT key value inserts or overwrites key.
  • GET key emits HIT value when the key exists, otherwise MISS. A hit increments that key's frequency by one.

After all operations, append STATS h m, where h and m are total hits and misses. Append each key with positive hit frequency as key:frequency in lexicographic key order. There is no capacity limit or eviction.

Function

trackCacheStatistics(operations: String[]) → String[]

Examples

Example 1

operations = ["PUT a 10","GET a","GET b","PUT b 20","GET b"]return = ["HIT 10","MISS","HIT 20","STATS 2 1 a:1 b:1"]

Both stored keys are hit once, and the first read of b is the only miss.

Example 2

operations = ["PUT x old","PUT x new","GET x","GET x"]return = ["HIT new","HIT new","STATS 2 0 x:2"]

The second PUT overwrites x without changing its read frequency.

Constraints

  • 0 <= operations.length <= 200000
  • Each command is exactly PUT key value or GET key.
  • Keys and values are nonempty ASCII tokens without whitespace.

More Goldman Sachs problems

  • Palindrome CounterOA · Seen Oct 2026
  • Longest Palindromic SubsequenceOA · Seen Oct 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
public String[] trackCacheStatistics(String[] operations) {
  // write your code here
}
Checking account…