Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Validation and Profit

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

Your question is Array Validation and Profit. 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

A Grubhub pricing analysis tool receives a chronological array of integer delivery prices. Given prices and a target, return three results: whether target appears at any zero-based even index, the maximum difference between any two values, and the maximum profit from one buy followed by one later sell.

The array is valid when at least one occurrence of target is located at an even index. The maximum difference is max(prices) - min(prices), regardless of order. The maximum profit must preserve chronological order, so buying and selling on the same day is not allowed. If no profitable transaction exists, return 0.

Formal Specification

Implement analyze_prices(prices, target). The input is a non-empty list of integers and an integer target. Return a dictionary with Boolean key valid, integer key max_difference, and integer key max_profit.

Constraints

  • 1 <= len(prices) <= 10^5
  • -10^9 <= prices[i], target <= 10^9
  • Indices are zero-based
  • Integer arithmetic is sufficient for all results

Function Signature

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