I recently got this question in a Snowflake interview and wanted to share it.
Problem Statement
You are given two datasets:
Products (as a vector of pairs):
Each entry contains:
product_id
price
Sales (as a list of records):
Each entry contains:
customer_id
product_id
quantity
Task
For each product, compute:
Number of unique customers who bought the product and also if one customer bought multiple product or if one prodcut was bought 1 guy and same product was bought by another guy then it should say 2 guy bought this product with the total revenue generated by the prodcut
Return/output in the format:
<number_of_customers> <product_id> <total_revenue>
Convert the products vector into a hashmap:
product_id → price
Iterate over sales:
Lookup price using product_id
Compute revenue = price × quantity
Aggregate revenue per product
Track unique customers using a set
Output results per product
// Given as vector of pairs
vector<pair<int, int>> products = {
{101, 10},
{102, 20},
{103, 15}
};
// sales: {customer_id, product_id, quantity}
vector<tuple<int, int, int>> sales = {
{1, 101, 5},
{2, 103, 2},
{1, 103, 1}
}
// Convert to map
unordered_map<int, int> priceMap;
for (auto &p : products) {
priceMap[p.first] = p.second;
}
unordered_map<int, long long> revenueMap;
unordered_map<int, unordered_set<int>> customersMap;
// Process sales
for (auto &[customer_id, product_id, quantity] : sales) {
int price = priceMap[product_id];
long long revenue = price * quantity;
revenueMap[product_id] += revenue;
customersMap[product_id].insert(customer_id);
}
// Output
for (auto &[product_id, revenue] : revenueMap) {
cout << customersMap[product_id].size() << " "
<< product_id << " "
<< revenue << endl;
}
return 0;}