📄 Need a professional CV? Try our Resume Builder! Get Started

Python OOPs Problems - Solutions

1. Design a Parking Lot System

Problem Description: Design a parking lot system where you can park and retrieve cars. The parking lot has a limited number of slots.

Methods:

  • park_car(car_number): Parks the car if a slot is available.
  • remove_car(car_number): Removes a parked car.
  • get_available_slots(): Returns the number of available slots.

Test Case:

Input:
parking_lot = ParkingLot(2)
parking_lot.park_car(101)
parking_lot.park_car(102)
parking_lot.get_available_slots()  # 0
parking_lot.remove_car(101)
parking_lot.get_available_slots()  # 1

Output: 0, 1

Code:

class ParkingLot:
    def __init__(self, total_slots):
        self.total_slots = total_slots
        self.occupied_slots = set()

    def park_car(self, car_number):
        if len(self.occupied_slots) < self.total_slots:
            self.occupied_slots.add(car_number)
            print(f"Car {car_number} parked.")
        else:
            print("Parking lot is full!")

    def remove_car(self, car_number):
        if car_number in self.occupied_slots:
            self.occupied_slots.remove(car_number)
            print(f"Car {car_number} removed.")
        else:
            print("Car not found in the parking lot!")

    def get_available_slots(self):
        return self.total_slots - len(self.occupied_slots)

parking_lot = ParkingLot(2)
parking_lot.park_car(101)
parking_lot.park_car(102)
print(parking_lot.get_available_slots())  # Output: 0
parking_lot.remove_car(101)
print(parking_lot.get_available_slots())  # Output: 1
                        

2. Bank Account System

Problem Description: Implement a BankAccount class with the following methods:

  • deposit(amount): Adds money to the account.
  • withdraw(amount): Deducts money if sufficient balance is available.
  • get_balance(): Returns the current balance.

Test Case:

Input:
account = BankAccount("John", 500)
account.deposit(200)
account.withdraw(100)
account.get_balance()

Output: 600, 500

Code:

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited {amount}. New balance: {self.balance}")

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
            print(f"Withdrew {amount}. New balance: {self.balance}")
        else:
            print("Insufficient funds!")

    def get_balance(self):
        return self.balance

account = BankAccount("John", 500)
account.deposit(200)  # Output: Deposited 200. New balance: 700
account.withdraw(100)  # Output: Withdrew 100. New balance: 600
print(account.get_balance())  # Output: 600
                        

3. Library Management System

Problem Description: Implement a library system where you can:

  • Add books.
  • Borrow books (if available).
  • Return books.

Test Case:

Input:
library = Library()
library.add_book("Python Basics", 3)
library.borrow_book("Python Basics")
library.get_book_count("Python Basics")
library.return_book("Python Basics")
library.get_book_count("Python Basics")

Output: 2, 3

Code:

class Library:
    def __init__(self):
        self.books = {}

    def add_book(self, title, count):
        if title in self.books:
            self.books[title] += count
        else:
            self.books[title] = count

    def borrow_book(self, title):
        if self.books.get(title, 0) > 0:
            self.books[title] -= 1
            print(f"Borrowed '{title}'.")
        else:
            print(f"'{title}' is not available!")

    def return_book(self, title):
        if title in self.books:
            self.books[title] += 1
            print(f"Returned '{title}'.")
        else:
            self.books[title] = 1

    def get_book_count(self, title):
        return self.books.get(title, 0)

library = Library()
library.add_book("Python Basics", 3)
library.borrow_book("Python Basics")
print(library.get_book_count("Python Basics"))  # Output: 2
library.return_book("Python Basics")
print(library.get_book_count("Python Basics"))  # Output: 3
                        

Nandi Vardhan
Data Scientist, content creator, and passionate educator in the field of machine learning and data science.