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
Group Anagrams and Max Subarray
00:00
5 left

Group Anagrams and Max Subarray

MediumPython

Problem

Coupa Spend Management receives supplier labels with inconsistent letter ordering and a chronological sequence of spend changes. Implement a function that groups labels that are anagrams and finds the largest sum of any non-empty contiguous range of spend changes.

Return a dictionary with anagram_groups and max_subarray_sum. Groups must appear in the order their signature is first encountered, and words within each group must preserve their input order. The spend result must be the maximum possible contiguous sum, even when every value is negative.

Formal Specification

Input consists of labels, a list of lowercase strings, and changes, a non-empty list of integers. Return {"anagram_groups": groups, "max_subarray_sum": maximum_sum}.

Example 1:

Input: labels = ["care", "race", "acre", "coupa"], changes = [4, -1, 2, 1, -6, 3]
Output: {"anagram_groups": [["care", "race", "acre"], ["coupa"]], "max_subarray_sum": 6}

The first three labels share a letter signature. The best contiguous change range is [4, -1, 2, 1], whose sum is 6.

Example 2:

Input: labels = ["ab", "ba", "xy"], changes = [-8, -3, -5]
Output: {"anagram_groups": [["ab", "ba"], ["xy"]], "max_subarray_sum": -3}

When all changes are negative, the answer is the least negative single value.

Constraints

  • 1 <= len(labels) <= 10^4
  • 1 <= len(labels[i]) <= 100
  • Labels contain only lowercase English letters
  • 1 <= len(changes) <= 10^5
  • -10^9 <= changes[i] <= 10^9

Function Signature

def analyze_spend(labels, changes):
Interviewer

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

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.