Implement a Credit Tracker Class

Last updated: September 4, 2025

Quick Overview

Implement a CreditTracker with add_credit, subtract_credit, and check_credit methods. Handle thread-safety for concurrent access.

Perplexity
Coding & Algorithms
Software Engineer
Perplexity
September 4, 2025
Software Engineer
Technical Phone Screen
Coding & Algorithms
Easy

14

4

2,709 solved


Implement a CreditTracker with add_credit, subtract_credit, and check_credit methods. Handle thread-safety for concurrent access.

Simple OOP problem reported at Perplexity. Tests basic class design with concurrency follow-ups for managing user API quotas.

What the Interviewer Expects
  • Implement clean class interface with proper encapsulation
  • Handle negative balance prevention
  • Add thread-safety with locks
  • Support multiple users with isolated balances
  • Write comprehensive edge case tests
Key Topics to Cover
OOP design
Thread safety
Concurrency
API rate limiting
Class encapsulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you persist credit balances across restarts?
  • What if credits expire after a time period?
  • How would you handle distributed credit tracking across multiple servers?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires implementing a CreditTracker class with methods to add, subtract, and check credits while ensuring thread safety. The primary pattern here is the use of thread locks to manage...

Approach
  1. Class Definition: We will define a CreditTracker class that holds a dictionary to maintain user balances.
  2. Method Implementations:
    • add_credit(user_id, amount): Increments the u...

Submit Your Answer
Markdown supported

Related Questions