Skip to main content
All Questions

Maximum Profit

Easy

You and your friend have decided to start an international trading business. You want to earn a profit through some ol' fashioned arbitrage: You'll buy widgets in one country and sell them in another at a higher price. Your friend has already found the current market prices for widgets in cities around the world, and you'd like to use this data to pick which cities to buy and sell widgets in.

Write a function max_profit(prices) that efficiently finds the two cities that maximize your profit (i.e. largest difference in prices).

Input: A dictionary with cities as keys and prices as values Output: An array containing the names of the cities (min, max)

If the prices dictionary is empty, return None/null (depending on the programming language used). If there is 0 profit to be earned, return None/null as well. If you are using C++, return {"", ""} in such cases.

Example

Python
prices = {'London': 72, 'New York': 70, 'Tokyo': 67, 'Miami': 62} max_profit(prices) # => ['Miami', 'London']

Related courses

Course

Data Analyst Interview Prep

Ace your data analyst interviews—whether you're targeting product, marketing, or business analyst roles. Tackle real take-home case studies, sharpen your technical and dashboarding skills, and get strategies from interviewers at top tech companies and startups.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.