Pinterest software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
CompressedGrid class that stores a sparse integer matrix efficiently by keeping only non-zero values in memory. The class must support...Input: Matrix (2D array)scores. Each row is sorted in nondecreasing order from left to right, and each column is sorted in nondecreasing order from top to bottom.Return the K largest entries in nonincreasing order. Each matrix cell is a separate entry: equal scores must appear multiple times if multiple such entries belong in the result. Return only score values, not cell coordinates. If k = 0, return an empty list.You must not modify the input matrix or flatten it into another collection. Your algorithm must use O(1) auxiliary space, excluding the returned list, which may also serve as its working buffer. In particular, a separate heap, visited set, or array of row pointers whose size grows with the input is not allowed.Target worst-case time is O((m + n) log(V + 1) + k log(k + 1)), where m and n are the matrix dimensions and V = maximum_score - minimum_score + 1. Standard library sorting is permitted only if its auxiliary-space guarantee meets the requirement.Function or class signaturetop_k_scores(scores: list[list[int]], k: int) -> list[int]{"scores": [[1, 4, 7], [2, 5, 8], [3, 6, 9]], "k": 4}[9, 8, 7, 6]
{"scores": [[-5, -1, 2], [-3, 2, 2], [0, 2, 7]], "k": 3}[7, 2, 2]
1 <= m, n <= 200000, with m * n <= 2000000.scores is rectangular and sorted along both dimensions as specified.[-10^9, 10^9].0 <= k <= min(m * n, 200000).k entries at any point.(score, row, column) triples, ordered by descending score, then ascending row, then ascending column. How would you select entries tied at the cutoff and preserve the same time and auxiliary-space bounds?Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free