FastPrepFind Most Frequently Purchased Products
Problems/Find Most Frequently Purchased Products

Find Most Frequently Purchased Products

HSBC logoHSBC● EasyOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

The manager of a grocery store tagGrocery wishes to determine which products are most popular with his customers (i.e. which products they purchase most frequently). The manager selects N customers who purchase a shopping bag of items containing M products, each labeled with a productID. By analyzing these M products, the manager wishes to find the productIDs of the distinct products that get purchased most frequently by at least K(given) customers.

Write an algorithm to help the manager find the productIDs of the products that are most frequently purchased by the K customers.

Input

  • The first line of the input consists of two space-separated integers - tag_row, tag_col, representing the number of customers (N), the number of products in the shopping bag of each customer (M).
  • The next N lines consist of M space-separated integers - tag[0], tag[1] .........., tag[M-1], representing the productIDs of the products contained in the shopping bag of each customer.
  • The last line consists of an integer- K_input, representing number of K customers who are chosen by the manager (K).

Output

Print space-separated integers representing the lexicographically sorted productIDs of the products that are most frequently purchased by the K customers.

Function

findMostFrequentlyPurchasedProducts(tag: int[][], K: int) → int[]

Examples

Example 1

tag = [[1, 2, 3, 2], [2, 3, 4, 8], [8, 3, 11, 12], [2, 3, 6, 8]]K = 3return = [2, 3, 8]

The products with ProductIDs 2, 3 and 8 are purchased by at least K customers.

So, the output is [2 3 8].

Constraints

  • 1 <= tag_row <= 10^3
  • 1 <= tag_col <= 10^3
  • 1 <= K_input < tag_row
  • 0 <= tag[0], tag[1], .........., tag[N-1] <= 10^9

More HSBC Online Assessment questions

  • Organization Reputation After Employee DeparturesOA · Seen Nov 2026
  • Remove Vowels from a StringOA · Seen Nov 2026
  • Minimum Deletion Cost for Distinct Adjacent CharactersOA · Seen Oct 2026
  • Distinct Products After Removing One ElementOA · Seen Sep 2026
  • Minimum Cleaning Robot RunsOA · Seen Sep 2026
  • Minimum Satellite Data Transfer IterationsOA · Seen Aug 2026
  • Find ID of SoldierOA · Seen Jun 2026
  • Minimum Markers to Clear Line SegmentsOA · Seen Nov 2025
  • Arrange Flower SticksOA · Seen Mar 2025
  • Find Largest House AreaOA · Seen Mar 2025

Similar questions at other companies

  • Build the Task HierarchyPendo · OA
  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
  • Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
  • Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions
  • Flat JSON Value DifferencesChubb · OAChubb interview questions
  • Items SortAkuna Capital · OAAkuna Capital interview questions
  • Count Cross-Array Target-Sum PairsVisa · OAVisa interview questions
  • Get Search ResultsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Lexicographic Word FrequenciesUpstart · OAUpstart interview questions
All HSBC interview questionsSee HSBC hiring insights
Saved
class Solution {
    public int[] findMostFrequentlyPurchasedProducts(int[][] tag, int K) {
      // write your code here
    }

}
Checking account…