Solve Triangle Rows and Top Frequencies
EricssonSoftware Engineer · Technical ScreenLast updated
This question set evaluates understanding of combinatorial number patterns and frequency-based selection, specifically the ability to generate an additive number triangle and to identify the k most frequent elements, assessing skills in array/list manipulation, counting, and selection algorithms.
You have 1 hour to solve both coding problems.
Problem 1: Build an Additive Number Triangle
Given an integer numRows, return the first numRows rows of a triangle of numbers with the following rules:
- Row indexing starts at
0. - The first and last value in every row are
1. - Each interior value is the sum of the two adjacent values directly above it:
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j].
Return the triangle as a list of rows.
Example:
Input: numRows = 5
Output: [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]
Problem 2: Return the K Most Frequent Values
Given an integer array nums and an integer k, return any k distinct values that appear most frequently in nums.
If multiple values have the same frequency, any valid set of k values is acceptable. The output may be returned in any order.
Example:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Expected approach: count frequencies and use a size-k min-heap to keep the current k most frequent values.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.