PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Salesforce

Implement an LFU cache

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of data structures and cache eviction policies, specifically frequency tracking for LFU caches and LRU tie-breaking among equal-frequency items.

  • hard
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Implement an LFU cache

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

## Problem: LFU Cache Design and implement a **Least Frequently Used (LFU) cache** with a fixed capacity. The cache supports the following operations: - `get(key) -> value` - Return the value of `key` if it exists in the cache. - Otherwise return `-1`. - If the key exists, its **usage frequency** increases by 1. - `put(key, value) -> void` - Insert or update the value of `key`. - If the cache reaches capacity, it must evict one key before inserting. ### Eviction Policy 1. Evict the key with the **lowest frequency** (LFU). 2. If multiple keys have the same (lowest) frequency, evict the **least recently used** (LRU) among them. ### Requirements / Constraints - Capacity is a non-negative integer. - Aim for **O(1)** average time per operation. - If `capacity == 0`, `put` does nothing and `get` always returns `-1`. ### Example If capacity = 2: - `put(1, 1)` - `put(2, 2)` - `get(1)` returns `1` (freq(1)=2) - `put(3, 3)` evicts key `2` (freq(2)=1 is lowest) - `get(2)` returns `-1` - `get(3)` returns `3`

Quick Answer: This question evaluates understanding of data structures and cache eviction policies, specifically frequency tracking for LFU caches and LRU tie-breaking among equal-frequency items.

Related Interview Questions

  • Solve Two OA Coding Problems - Salesforce (medium)
  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Minimize operations to reduce integer to zero - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)
Salesforce logo
Salesforce
Dec 15, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
9
0

Problem: LFU Cache

Design and implement a Least Frequently Used (LFU) cache with a fixed capacity.

The cache supports the following operations:

  • get(key) -> value
    • Return the value of key if it exists in the cache.
    • Otherwise return -1 .
    • If the key exists, its usage frequency increases by 1.
  • put(key, value) -> void
    • Insert or update the value of key .
    • If the cache reaches capacity, it must evict one key before inserting.

Eviction Policy

  1. Evict the key with the lowest frequency (LFU).
  2. If multiple keys have the same (lowest) frequency, evict the least recently used (LRU) among them.

Requirements / Constraints

  • Capacity is a non-negative integer.
  • Aim for O(1) average time per operation.
  • If capacity == 0 , put does nothing and get always returns -1 .

Example

If capacity = 2:

  • put(1, 1)
  • put(2, 2)
  • get(1) returns 1 (freq(1)=2)
  • put(3, 3) evicts key 2 (freq(2)=1 is lowest)
  • get(2) returns -1
  • get(3) returns 3

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Salesforce•More Software Engineer•Salesforce Software Engineer•Salesforce Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.