FastPrepREST API: Energy Bar
Problems/REST API: Energy Bar

REST API: Energy Bar

Rippling logoRippling● MediumNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source specifies the calorie/rounded-average-weight formula, rounded-price tie and brand:type output; its France sample is Patchi:Caramel Chocolate with energy 384 and average price 382. Combined-page arrays replace HTTP access, and no-match/remaining ties/bounds are practice conventions. The literal formula is retained despite the per-gram wording; source match is about 95%.

Select the chocolate record from the requested country that supplies the most energy under the formula below. The original task retrieves paginated API records; this offline version receives the combined records from all pages in aligned arrays.

For record i, require an exact, case-sensitive match of countries[i] and country. Let W = floor(sum(weights[i]) / weights[i].length), P = floor(sum(prices[i]) / prices[i].length), and E = floor(kcals[i] * W / 100). Maximize E, breaking ties by smaller P. If both remain equal, choose the first input record.

Return its brands[i] + ":" + types[i]. Return an empty string if no record matches. Compute sums and products with signed 64-bit arithmetic. The source formula governs selection even though its prose calls the score energy per gram.

Function

energyBar(country: String, countries: String[], brands: String[], types: String[], kcals: int[], weights: int[][], prices: int[][]) → String

Examples

Example 1

country = "Norway"countries = ["Norway"]brands = ["Tony's Chocolonely"]types = ["Truffle"]kcals = [115]weights = [[125,71,116,100,126]]prices = [[614,283,564,365,689]]return = "Tony's Chocolonely:Truffle"

The shown record has floor-average weight 107, energy floor(115*107/100)=123 and floor-average price 503.

Example 2

country = "France"countries = ["France","France","Norway"]brands = ["A","B","C"]types = ["Dark","Milk","Nut"]kcals = [101,100,1000]weights = [[100],[101],[100]]prices = [[11,10],[9],[1]]return = "B:Milk"

A and B each supply energy 101. B has lower floor-average price; the Norway record is excluded.

Example 3

country = "Spain"countries = ["France"]brands = ["A"]types = ["Dark"]kcals = [100]weights = [[100]]prices = [[5]]return = ""

No country matches exactly.

Constraints

  • All record arrays and both matrices have the same row count 0 <= n <= 500.
  • Each weight or price row has 1..100 entries, each in 1..1000000000; 1 <= kcals[i] <= 1000000000.
  • Country, brand and type strings contain 1..80 printable ASCII characters and no colon.

More Rippling problems

  • Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
  • Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
All Rippling interview questionsSee Rippling hiring insights
Saved
public String energyBar(String country, String[] countries, String[] brands, String[] types, int[] kcals, int[][] weights, int[][] prices) {
    // Write your code here.
}

Not a valid String[]

Checking account…