Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Maximum Money in Consecutive Bags
00:00
5 left

Maximum Money in Consecutive Bags

MediumSQL · PostgreSQL

Problem

Maximum Money in K Consecutive Bags: In Amazon's financial team, an analyst is dealing with an infinite number of bags arranged in a line, each numbered from 1 to infinity. The task is to gather information about the amount of money in these bags, presented as continuous segments. The objective is to select consecutive bags with the maximum total amount of money. Return the answer modulo 10^9 + 7.

Asked in the OA stage. The input is stored in continuous segments in bag_segments, with K set in the query.

Output

  1. Return one row with max_total_money.
  2. The value must represent the maximum total across exactly K consecutive bags, modulo 1,000,000,007.

Output details

  • Use K = 5 for the consecutive-bag window length.
  • Interpret start_bag and end_bag as inclusive endpoints.
  • Apply each segment's money_per_bag to every bag in its inclusive range.
  • Return the nonnegative remainder modulo 1000000007.

Schema

bag_segments
ColumnTypeDescription
segment_idPKINTUnique identifier for the continuous segment
start_bagBIGINTFirst bag number in the segment
end_bagBIGINTLast bag number in the segment
money_per_bagBIGINTMoney contained in every bag in the segment
segment_labelVARCHAR(80)Optional descriptive label for the segment
Tablesbag_segments
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.