Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Cart Operations With Undo/Redo
00:00
5 left

Cart Operations With Undo/Redo

HardPython

Problem

Implement add_recipe, remove_recipe, get_total_discount, and then add support for undo and redo of cart operations.

Implement a cart object that supports adding and removing recipes, computing the current total discount, and undoing or redoing the most recent cart changes. Assume each recipe has a unique recipe_id and a numeric discount value.

Required operations

  1. add_recipe(recipe_id, discount) adds or updates a recipe in the cart.
  2. remove_recipe(recipe_id) removes a recipe if it exists.
  3. get_total_discount() returns the sum of discounts for all recipes currently in the cart.
  4. undo() reverts the most recent add or remove operation.
  5. redo() reapplies the most recently undone operation.

If an operation cannot be applied, it should do nothing. Return the current total discount after each get_total_discount() call.

Constraints

  • 1 <= len(operations) <= 10^5
  • recipe_id is an integer in the range [1, 10^9]
  • discount is an integer in the range [0, 10^9]
  • Each operation is one of: add_recipe, remove_recipe, get_total_discount, undo, redo
  • Undo and redo only affect successful add_recipe and remove_recipe operations

Function Signature

def manage_cart(operations):
Interviewer

Your question is Cart Operations With Undo/Redo. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.