Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top 3 Companies by Stock Averages
00:00
5 left

Top 3 Companies by Stock Averages

EasyPython

Problem

A financial insights prototype connected to Shopify Admin receives one week of stock prices for multiple companies. Find the three companies with the highest average stock price over the seven-day period.

Return their zero-based row indices, ordered from highest average to lowest. If two companies have the same average, place the company with the smaller index first.

Formal Specification

Implement top_three_companies(prices), where prices is a non-empty 2D array of numbers. Each row represents one company, and each row contains exactly seven daily prices. Return a list containing up to three integer row indices. If fewer than three companies are provided, return all available indices.

You may compare seven-day totals instead of averages because every company has the same number of days.

Constraints

  • 1 <= len(prices) <= 10^5
  • Each company has exactly 7 daily prices
  • 0 <= prices[i][j] <= 10^9
  • Prices may be integers or floating-point numbers
  • Return at most three indices

Function Signature

def top_three_companies(prices):
Interviewer

Your question is Top 3 Companies by Stock Averages. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.