FastPrepItem Prices From Material Costs
Problems/Item Prices From Material Costs

Item Prices From Material Costs

C3.ai logoC3.ai● EasyNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In a 2020 report on a C3.ai Software Engineer - Early Professional loop, the first interview listed after the online test was a pricing question. You work out what each item costs from the materials it needs, how much of each, and the material prices. The report rates the question medium to hard but gives nothing more.

The report gives no input format, no rule for a material without a price, and no limits. Here, each item lists its materials and amounts, and its price is the sum of amount times unit price. An item that needs a material with no listed price gets -1, and an item with no materials costs 0. Prices can go past the 32-bit range. This direct version is likely simpler than what was asked, and the limits are our choice.

Our version is about 50% the same as the reported question.

A workshop buys materials by the unit. Material materials[j] costs prices[j] per unit.

There are m items to price. Item i needs itemQuantities[i][k] units of material itemMaterials[i][k], for every k. The price of an item is the total cost of the materials it needs: the sum of amount times unit price.

Return an array answer of length m, where answer[i] is the price of item i.

  • If item i needs a material that does not appear in materials, its price cannot be worked out, and answer[i] is -1.
  • An item that needs no materials costs 0.

Function

itemPrices(materials: String[], prices: int[], itemMaterials: String[][], itemQuantities: int[][]) → long[]

Examples

Example 1

materials = ["wood","nail","glue"]prices = [5,1,3]itemMaterials = [["wood","nail"],["glue","wood"],["nail"]]itemQuantities = [[2,10],[1,4],[25]]return = [20,23,25]

Item 0 needs 2 wood and 10 nails: 2 * 5 + 10 * 1 = 20. Item 1 needs 1 glue and 4 wood: 1 * 3 + 4 * 5 = 23. Item 2 needs 25 nails: 25 * 1 = 25.

Example 2

materials = ["steel","rubber"]prices = [40,7]itemMaterials = [["steel","paint"],["rubber"],[]]itemQuantities = [[1,2],[4],[]]return = [-1,28,0]

Item 0 needs paint, which has no price in the list, so its price is -1. Item 1 costs 4 * 7 = 28. Item 2 needs nothing, so it costs 0.

Example 3

materials = ["gold","silk"]prices = [1000000,250]itemMaterials = [["silk","gold"]]itemQuantities = [[10000,10000]]return = [10002500000]

The single item costs 10000 * 250 + 10000 * 1000000 = 10002500000. That is larger than the largest 32-bit integer, 2147483647, so the sum needs a 64-bit integer.

Constraints

  • 1 <= materials.length <= 1000
  • prices.length == materials.length
  • 1 <= prices[j] <= 10^6
  • 1 <= itemMaterials.length <= 500
  • itemQuantities.length == itemMaterials.length
  • 0 <= itemMaterials[i].length <= 50, and itemQuantities[i].length == itemMaterials[i].length
  • 1 <= itemQuantities[i][k] <= 10^4
  • Every name in materials and itemMaterials has 1 to 10 lowercase English letters.
  • The names in materials are all different, and the names in each itemMaterials[i] are all different.

More C3.ai interview questions

  • Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024
  • Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
  • Longest Run of Ones After Flipping k ZerosPHONE SCREEN · Seen Mar 2022
  • Top K Frequent ElementsPHONE SCREEN · Seen Feb 2021
  • Adjacent-Key Lock PatternsPHONE SCREEN · Seen Feb 2014
  • Drop Recent Duplicate MessagesOA · Seen Oct 2026
  • Maximum K-Star SumOA · Seen Oct 2026
  • Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
  • Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
  • Rotate a Square Grid ClockwiseONSITE INTERVIEW · Seen Jan 2023

Similar questions at other companies

  • Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
  • Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
  • Most Frequent NameAmazon · PHONE SCREEN
  • Words with Repeated AnagramsThe Voleon Group · PHONE SCREEN
  • Project JSON Fields into a Target FormatBooking.com · PHONE SCREEN
  • Product Keyword SearchTesco · PHONE SCREEN
  • Verify Words Sorted by a Custom AlphabetTrueFoundry · PHONE SCREEN
  • Character Frequencies Across Nested String ListsSnowflake · PHONE SCREENSnowflake interview questions

Interview reports that mention this question

  • C3.ai New Grad Interviews: 25 Experiences (Part 1 of 2)

C3.ai interview experiences

  • C3.ai FDE and Other New Grad Roles: 5 Experiences (Part 2 of 2)
  • C3.ai New Grad Forward Deployed Engineer Interview: 1 Experience
All C3.ai interview questionsSee C3.ai hiring insights
Saved
class Solution {
    public long[] itemPrices(String[] materials, int[] prices, String[][] itemMaterials, int[][] itemQuantities) {
        // Write your code here.
    }
}
Checking account…