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

Optimize Code Time and Space

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

Whenever you're ready, walk me through how you'd approach this.

You need to log in / sign up to chat or submit.

Problem

Lowe's runs seasonal bundle promotions where two SKUs whose prices sum exactly to a promo total get auto-linked as a suggested bundle. A merchandising analyst wrote the function below to find those matching price pairs from a store's price list, but on a full department price list (several thousand SKUs) it takes minutes to run and burns memory the team can't account for.

def find_bundle_pairs(prices, target_total):
    all_pairs = []
    for i in range(len(prices)):
        for j in range(len(prices)):
            if i != j:
                all_pairs.append((i, j))

    matches = []
    for (i, j) in all_pairs:
        if prices[i] + prices[j] == target_total:
            matches.append((i, j))

    unique_matches = []
    for pair in matches:
        if pair not in unique_matches and (pair[1], pair[0]) not in unique_matches:
            unique_matches.append(pair)

    return unique_matches

Explain the time and space complexity problems in this function and describe exactly how you would rewrite it so it scales to a full department price list.