Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Simple Price Word Problem

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Simple Price Word Problem. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Sparrow Foundation maintains a small produce price catalog. Given a dictionary mapping fruit names to prices in cents and a requested fruit name, return that fruit's price.

Implement fruit_price(prices, fruit). The requested fruit is guaranteed to exist in the catalog, so no missing-key behavior is required.

Formal Specification

  • Input: prices, a dictionary whose keys are lowercase fruit names and whose values are non-negative integer prices in cents; fruit, a lowercase string.
  • Output: Return the integer price associated with fruit.
  • Requirement: Use the fruit name as the dictionary key. Do not infer a price from the prices of other fruits.

Constraints

  • 1 <= len(prices) <= 10^4
  • Each fruit name contains 1 to 30 lowercase English letters
  • 0 <= prices[name] <= 10^6
  • fruit is guaranteed to be a key in prices

Function Signature

def fruit_price(prices, fruit):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output