← LinkedIn Interview Insights

LinkedIn·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
Jun 2026

Summary

LinkedIn coding round for a software engineer role, three algorithmic problems back to back. The problems weren't exactly easy and complexity analysis was expected for all of them.

Questions Asked (3)

Q1

Design a cache that supports get and put operations in O(1) average time, evicting the least frequently used key when full. Ties in frequency should be broken by recency.

Algorithms & Data StructuresSystem Design
Author's notes

This is the LFU cache problem and it's nasty if you haven't seen it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: O(1) average time for get and put, eviction of least frequently used key, and ties broken by recency. Then propose a design combining a hash map for O(1) access to nodes and a frequency-based doubly linked list structure (or a min-heap with lazy updates) to track LFU order. Walk through the get and put operations, explaining how frequency updates and eviction work in O(1) average time.

Pro tip: Mention that a naive min-heap gives O(log n) for updates, so to achieve O(1) average you need a hash map plus a doubly linked list of frequency buckets, each containing a doubly linked list of keys with that frequency. This shows you understand the trade-offs and can optimize beyond the obvious.

1. Clarify requirements and constraints

Confirm that get and put must be O(1) average, eviction is LFU with LRU tie-breaking, and discuss edge cases like capacity 0 or 1, and updating existing keys.

2. Choose data structures

Use a hash map for O(1) key lookup, mapping to nodes. Use a doubly linked list of frequency buckets, each bucket containing a doubly linked list of keys with that frequency, ordered by recency.

3. Define node and bucket structures

Each node stores key, value, frequency, and pointers to prev/next in its frequency list. Each bucket stores frequency and pointers to prev/next bucket, plus head/tail of its key list.

4. Implement get and put operations

For get: if key exists, increment its frequency, move it to the appropriate bucket (create if needed), and return value. For put: if key exists, update value and increment frequency; if new, insert with frequency 1; if capacity exceeded, evict the least frequent and least recently used key (tail of lowest frequency bucket).

5. Analyze complexity and edge cases

Explain that all operations are O(1) average due to hash map and constant-time list manipulations. Discuss handling of capacity limits, updating existing keys, and tie-breaking by recency.

Key Points to Mention

  • Hash map for O(1) access to nodes
  • Doubly linked list of frequency buckets to maintain LFU order
  • Within each frequency bucket, a doubly linked list to maintain recency order (LRU tie-breaking)
  • Incrementing frequency moves node to next bucket, creating it if necessary
  • Eviction removes the least recently used node from the lowest frequency bucket
  • Time complexity: O(1) average for get and put, space O(capacity)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Given two strings, find the shortest contiguous substring of the first string that contains all characters of the second string, including duplicates. Return an empty string if none exists.

Algorithms & Data Structures
Author's notes

Sliding window, classic minimum window substring.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window with two pointers to expand and contract the window while tracking character frequencies. Maintain a count of matched characters to know when the window contains all required characters, and update the minimum length when a valid window is found.

Pro tip: Clarify edge cases upfront (e.g., empty strings, case sensitivity, character set) and discuss trade-offs between approaches (e.g., sliding window vs. brute force). Mention that the sliding window approach is optimal with O(n) time and O(k) space, where k is the number of unique characters in the second string.

1. Understand the problem and edge cases

Restate the problem in your own words and ask clarifying questions about input constraints, character set, and expected output. Identify edge cases such as empty strings, no valid substring, or duplicate characters.

2. Choose the right algorithm

Explain that a brute-force approach would be O(n^2 * m) and inefficient. Propose the sliding window technique with frequency maps to achieve O(n) time.

3. Outline the sliding window approach

Describe how to use two pointers (left and right) to represent a window. Expand right to include characters until the window is valid, then contract left to minimize the window while keeping it valid.

4. Detail the frequency tracking and validation

Explain how to maintain a frequency map of the target string and a count of matched characters. When the count equals the number of unique characters in the target, the window is valid.

5. Analyze complexity and test with examples

State the time and space complexity (O(n) time, O(k) space). Walk through a small example to demonstrate correctness and handle edge cases.

Key Points to Mention

  • Sliding window technique with two pointers
  • Frequency maps (hash maps or arrays) to track character counts
  • Matched character count to validate window
  • Time complexity O(n) and space complexity O(k)
  • Handling duplicates and edge cases (empty strings, no solution)
  • Comparison with brute-force approach to highlight efficiency

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Given two sorted integer arrays and an integer k, return the k pairs (one element from each array) with the smallest sums. Return all pairs if fewer than k exist.

Algorithms & Data Structures
Author's notes

Min-heap approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a min-heap to efficiently merge the two sorted arrays by sums, starting with the smallest possible pair (0,0). Push the next candidates (i+1,j) and (i,j+1) into the heap, avoiding duplicates, and extract k pairs. This approach runs in O(k log k) time and handles the case where fewer than k pairs exist by returning all extracted pairs.

Pro tip: Clarify with the interviewer whether the arrays can contain duplicates and if the output should be sorted by sum; also mention that using a visited set or index tracking prevents duplicate pairs in the heap.

1. Clarify requirements and edge cases

Ask about input constraints (array sizes, possible duplicates, k value) and expected output format (order of pairs, handling fewer than k pairs).

2. Choose the right data structure

Select a min-heap (priority queue) to efficiently retrieve the smallest sum pairs, since the arrays are sorted and we need the k smallest sums.

3. Initialize and iterate

Push the initial pair (0,0) into the heap. While the heap is not empty and we have fewer than k pairs, pop the smallest sum, add it to the result, and push the next possible pairs (i+1,j) and (i,j+1) if within bounds and not already visited.

4. Handle duplicates and bounds

Use a visited set or a boolean matrix to avoid pushing the same pair multiple times. Ensure indices do not exceed array lengths.

5. Return the result

After extracting up to k pairs, return the list of pairs. If fewer than k pairs exist, return all extracted pairs.

Key Points to Mention

  • Time complexity: O(k log k) due to heap operations, which is optimal for this problem.
  • Space complexity: O(k) for the heap and visited set, which is efficient.
  • Handling duplicates: Use a visited set or index tracking to avoid duplicate pairs in the heap.
  • Edge cases: empty arrays, k=0, k larger than total pairs, and arrays with negative numbers.
  • Alternative approaches: brute force with sorting all pairs (O(mn log mn)) is inefficient; heap approach is better.
  • Correctness: The heap always contains the next smallest candidates because the arrays are sorted, ensuring we get the k smallest sums.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.