Problem · Sorting

Customer Revenue with Direct Referrals

Learn this problem
MediumDatabricks logoDatabricksFULLTIMEPHONE SCREEN

Problem statement

Process a finite ordered sequence of customer-revenue operations. Customer IDs are assigned automatically as 0, 1, 2, ....

  • ["ADD", revenue] creates a customer whose total revenue starts at revenue.
  • ["ADD_WITH_REFERRER", revenue, referrerId] creates a customer and immediately adds that initial revenue once to the existing direct referrer's total. The credit does not propagate to any earlier referrer.
  • ["LOWEST_K", k, threshold] returns up to k customer IDs whose current totals are at least threshold, ordered by total revenue ascending and then customer ID ascending.

Return one integer array per operation: an addition returns a one-element array containing the new ID, and a query returns its ordered customer IDs.

Function

processCustomerRevenueOperations(operations: String[][]) → int[][]

Examples

Example 1

operations = [["ADD","100"],["ADD","40"],["LOWEST_K","2","0"]]return = [[0],[1],[1,0]]

Customer 1 has total 40 and customer 0 has total 100, so the query returns them in that order.

Example 2

operations = [["ADD","50"],["ADD_WITH_REFERRER","20","0"],["ADD_WITH_REFERRER","30","0"],["LOWEST_K","3","25"],["ADD_WITH_REFERRER","40","1"],["LOWEST_K","4","0"]]return = [[0],[1],[2],[2,0],[3],[2,3,1,0]]

The first two referrals raise customer 0 to 100. The last referral raises customer 1 from 20 to 60 but does not propagate to customer 0.

Example 3

operations = [["ADD","10"],["ADD","10"],["ADD_WITH_REFERRER","0","0"],["LOWEST_K","2","10"]]return = [[0],[1],[2],[0,1]]

The threshold is inclusive. Customers 0 and 1 tie at total 10, so customer ID breaks the tie.

Constraints

  • 1 <= operations.length <= 5000.
  • Every operation is well formed and uses one of the three documented names.
  • Every revenue and threshold is a decimal integer in [0, 1000000000].
  • Every referrer ID names a customer created by an earlier operation.
  • 0 <= k <= 5000.
  • Every customer total fits a signed 64-bit integer.

More Databricks problems

drafts saved locally
public int[][] processCustomerRevenueOperations(String[][] operations) {
  // write your code here
}
operations[["ADD","100"],["ADD","40"],["LOWEST_K","2","0"]]
expected[[0],[1],[1,0]]
checking account