FastPrepFilter Linked-List Nodes and Keep Top K
Problems/Filter Linked-List Nodes and Keep Top K

Filter Linked-List Nodes and Keep Top K

Google logoGoogle● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The Google phone report says to pull out the matching linked-list nodes and then solve a top-k follow-up. The author compares a heap with quickselect. It does not say what the condition is, what value ranks the nodes, or what form to return. Here we use an integer threshold, pick the greatest values, and return a list in the original order. Our version is about 65% the same as the reported filter-and-select task.

Traverse a finite singly linked list and consider only nodes whose value is at least minimum. Among those matching nodes, select the k greatest values and return a new linked list containing their values in original input order. The input list is not modified.

  • Duplicate nodes count separately. If equal values lie at the selection cutoff, choose earlier matching nodes first.
  • If fewer than k nodes match, return every matching node. If none match or k is zero, return an empty list.

Function

extractTopKMatching(head: ListNode, minimum: int, k: int) → ListNode

Examples

Example 1

head = [4, 9, 4, 8, 4]minimum = 4k = 3return = [4, 9, 8]
head
49484
Removed
→
return
498
Moved or new

The two larger values are 9 and 8. The earliest of three tied 4-valued nodes fills the third slot, and selected values keep list order.

Example 2

head = [2, -1, 7, 7, 5]minimum = 3k = 2return = [7, 7]
head
2-1775
Removed
→
return
77
Moved or new

Only 7, 7 and 5 pass the threshold; the top two are the two 7-valued nodes.

Example 3

head = [1, 2, 3]minimum = 10k = 2return = []
head
123
return
empty

No node reaches the minimum, so the extracted list is empty.

Example 4

head = []minimum = 0k = 2return = []
head
empty
return
empty

An empty input list has no matching nodes.

Constraints

  • The input list is acyclic and has 0 through 20000 nodes.
  • Node values and minimum are integers in [-10^9, 10^9].
  • 0 <= k <= 20000.

More Google Phone Screen questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Merge Timestamped Record ListsNuro · PHONE SCREEN
  • Build a Sorted Circular Singly Linked ListAnduril · PHONE SCREEN
  • Sort a Linked ListAdobe · PHONE SCREEN
  • Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
  • Minimize Task Resource ConsumptionNVIDIA · OA
  • Sort a Linked List in Descending OrderMicrosoft · ONSITE INTERVIEW
  • Chain MergeRubrik · OA
  • Sort an Absolute-Value-Sorted Linked ListBloomberg LP · ONSITE INTERVIEW
  • Merge Sort a Linked ListZillow · ONSITE INTERVIEW
  • Convert a Sorted Linked List to a Balanced BSTMeta · PHONE SCREEN
See Google hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode extractTopKMatching(ListNode head, int minimum, int k) {
        // Write your code here.
    }
}
Checking account…