PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Capital One

Fix failing tests and refactor code

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to debug, test, and refactor Python code with attention to state management, mutable default arguments, deterministic unit testing, API design, and type-safety.

  • Medium
  • Capital One
  • Coding & Algorithms
  • Data Scientist

Fix failing tests and refactor code

Company: Capital One

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You're given a small Python repo. After creating and activating a clean virtual environment, installing requirements, and running "pytest -q", two tests intermittently fail with state leakage. Consider this simplified core function: # file: cart.py STATE = {} def add_item(cart=None, item_id=None, qty=1, price=0.0, discounts=[]): if cart is None: cart = STATE if item_id in cart: cart[item_id]['qty'] += qty else: cart[item_id] = {'qty': qty, 'price': price, 'discounts': discounts} total = sum(v['qty']*v['price'] for v in cart.values()) total -= sum(d for v in cart.values() for d in v['discounts']) return total Tasks: 1) Precisely explain what this function does on a single call and over multiple calls; identify at least three concrete design problems that can cause flakiness or poor readability (e.g., shared mutable defaults, hidden global state, mixing state mutation with aggregation). 2) Write a minimal pytest test that reliably exposes the shared-mutable-default bug for discounts (no external files; use plain asserts). 3) Refactor add_item to eliminate global state and mutable defaults, improve readability and type safety (e.g., dataclasses or simple dicts with type hints), and make the function pure where appropriate. Briefly justify API/structure changes. 4) Show how you would organize tests and fixtures (e.g., tmp_path, monkeypatch) and mock time or randomness if needed; explain how you'd make the suite deterministic. 5) List the exact shell commands you would run for environment setup, dependency pinning for reproducibility, and running tests with coverage locally and in CI.

Quick Answer: This question evaluates a candidate's ability to debug, test, and refactor Python code with attention to state management, mutable default arguments, deterministic unit testing, API design, and type-safety.

Related Interview Questions

  • Solve Four Coding Assessment Tasks - Capital One (medium)
  • Write SQL using joins and window functions - Capital One (medium)
  • Review Preprocessing Code and Tests - Capital One (easy)
  • Remove nodes with a given value - Capital One (medium)
  • Solve multiple algorithmic interview questions - Capital One (hard)
Capital One logo
Capital One
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Coding & Algorithms
6
0

You're given a small Python repo. After creating and activating a clean virtual environment, installing requirements, and running "pytest -q", two tests intermittently fail with state leakage. Consider this simplified core function:

file: cart.py

STATE = {} def add_item(cart=None, item_id=None, qty=1, price=0.0, discounts=[]): if cart is None: cart = STATE if item_id in cart: cart[item_id]['qty'] += qty else: cart[item_id] = {'qty': qty, 'price': price, 'discounts': discounts} total = sum(v['qty']*v['price'] for v in cart.values()) total -= sum(d for v in cart.values() for d in v['discounts']) return total

Tasks:

  1. Precisely explain what this function does on a single call and over multiple calls; identify at least three concrete design problems that can cause flakiness or poor readability (e.g., shared mutable defaults, hidden global state, mixing state mutation with aggregation).
  2. Write a minimal pytest test that reliably exposes the shared-mutable-default bug for discounts (no external files; use plain asserts).
  3. Refactor add_item to eliminate global state and mutable defaults, improve readability and type safety (e.g., dataclasses or simple dicts with type hints), and make the function pure where appropriate. Briefly justify API/structure changes.
  4. Show how you would organize tests and fixtures (e.g., tmp_path, monkeypatch) and mock time or randomness if needed; explain how you'd make the suite deterministic.
  5. List the exact shell commands you would run for environment setup, dependency pinning for reproducibility, and running tests with coverage locally and in CI.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Capital One•More Data Scientist•Capital One Data Scientist•Capital One Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.