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
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
Prevent Negative Seller Balance
00:00
5 left

Prevent Negative Seller Balance

MediumPython

Problem

Implement a safeguard logic that ensures the seller’s wallet balance cannot fall into a negative state.

Use safeguard_wallet(initial_balance, transactions). transactions is an array of signed integers: positive values add funds and negative values request debits. Process transactions in order, accept a transaction only if the resulting balance is non-negative, and return the final balance plus a Boolean acceptance result for every transaction.

Example 1: initial_balance = 100, transactions = [-30, -80, 50] returns {"final_balance": 120, "accepted": [true, false, true]}.

Example 2: initial_balance = 0, transactions = [-1, 10] returns {"final_balance": 10, "accepted": [false, true]}.

Constraints: the initial balance and transaction values are integers, the initial balance is non-negative, and transaction count is at most 10^5.

Constraints

  • 0 <= initial_balance <= 10^12
  • 0 <= transactions.length <= 10^5
  • -10^12 <= transactions[i] <= 10^12
  • All balances and transaction values are integers
  • The initial balance is non-negative

Function Signature

def safeguard_wallet(initial_balance, transactions):
Interviewer

Your question is Prevent Negative Seller Balance. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.