FastPrepTop K Pins with Insert and Query
Problems/Top K Pins with Insert and Query

Top K Pins with Insert and Query

Pinterest logoPinterest● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In a Pinterest phone screen for an experienced software engineer, the coding question was a top-k pins task: pins carry scores, new pins keep arriving, and the interviewer asks for the k best pins, with k changing from one query to the next. The discussion went from sorting a full list, to a heap for a stream, to a self-balancing sorted structure, because a heap has to take pins out and put them back for every query of a new size. The candidate then had to write the insert and the top-k query.

The report gives no input format, no rule for equal scores, no limits and no behavior for a k larger than the number of pins. Here, the calls come as one ordered list of insert and top operations; pin IDs are unique and a pin is never removed or rescored; equal scores rank the smaller ID first; each top query records one list of pin IDs, best first, and the lists come back in the order of the top queries (an empty array when there is none); a top query with k above the pin count returns every pin; a query never removes pins; and the limits and examples are ours.

The judged task matches the report at about 80%.

A pin board holds pins. Each pin has a unique integer ID and an integer score. Process the operations operations[0], operations[1], ... in order. values[i] holds the two numbers used by operations[i]:

  • "insert": values[i] = [pinId, score]. Add a pin with this ID and score. Pins are never removed, and their scores never change.
  • "top": values[i] = [k, 0]. Record the IDs of the k best pins currently on the board, best first. A pin ranks higher when its score is higher. When two pins have the same score, the pin with the smaller ID ranks higher. If the board holds fewer than k pins, record all of them in rank order (an empty list if there are none).

A "top" operation only reads the board; it does not remove any pin. The value of k can be different for every query.

Return the recorded lists in order, one list per "top" operation. If there is no "top" operation, return an empty array.

Function

topKPins(operations: String[], values: int[][]) → int[][]

Examples

Example 1

operations = ["insert","insert","insert","top","insert","top","top"]values = [[1,50],[2,80],[3,50],[2,0],[4,90],[3,0],[1,0]]return = [[2,1],[4,2,1],[4]]
  • After three inserts the pins rank as 2 (score 80), then 1 and 3 (both score 50). Pin 1 comes before pin 3 because the scores tie and 1 is the smaller ID.
  • top 2 records [2,1].
  • Pin 4 with score 90 is now the highest, so top 3 records [4,2,1].
  • A query does not remove pins, so top 1 still finds pin 4 and records [4].

Example 2

operations = ["top","insert","insert","top"]values = [[3,0],[7,-5],[5,-5],[5,0]]return = [[],[5,7]]
  • The first top 3 runs before any insert, so it records an empty list.
  • Pins 7 and 5 both have score -5. Pin 5 was inserted later but has the smaller ID, so it comes first.
  • top 5 asks for more pins than exist, so it records all of them: [5,7].

Example 3

operations = ["insert","insert","insert","insert"]values = [[10,3],[20,9],[30,1],[40,7]]return = []

There is no top operation, so nothing is recorded and the answer is an empty array.

Constraints

  • 1 <= operations.length <= 3000
  • values.length == operations.length
  • values[i].length == 2
  • Each operations[i] is "insert" or "top".
  • -10^9 <= values[i][j] <= 10^9
  • For an insert, values[i] = [pinId, score] with 0 <= pinId <= 10^6.
  • All inserted pin IDs are distinct.
  • For a top, values[i] = [k, 0] with 1 <= k <= 1000.
  • The sum of k over all top operations is at most 1000.

More Pinterest problems

  • Gaussian Naive Bayes Classifier (Machine Learning OA)OA · Seen Nov 2026
  • Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
  • Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
All Pinterest interview questionsSee Pinterest hiring insights
Saved
class Solution {
    public int[][] topKPins(String[] operations, int[][] values) {
        // Write your code here.
    }
}
Checking account…