Python Developer Interview Questions

6,401 python developer interview questions shared by candidates

# Shopping cart It is a partial implementation of a shopping till system, which you might find at a supermarket. This implementation was done by a Junior developer, you as a Senior Software Engineer have been requested to refactor this project. You may make any technical decisions you would like, but must not change the given abstract class (abc.ShoppingCart) which is used by the shopping till hardware and cannot be easily updated. Please treat this code as an element of a larger production system. The code is being refactored to ensure reliability and testability. Tasks requested: - Make the receipt print items in the order that they were added - Add a 'Total' line to the receipt. This should be the full price we should charge the customer - Be able to fetch product prices from an external source (json file, database ...) - Be able to display the product prices in different currencies (not only Euro). - Update the test suite to extend coverage and limit the number of tests which need changing when changes are introduced - Any other changes which improve the reliability of this code in production If you do not have enough information, make any assumptions you would like and note them down with TODO comments. Feel free to make comments that highlight completion of the tasks listed above. Please budget 3 hours to complete. We understand you may have other commitments and time constraints and do evaluate responses with the stated level of effort in mind. Please let me know (roughly) when we should expect your answers or if you need more time. We strive to respond to your effort as promptly as we can. Please don’t forget, your code should be production ready, clean and tested! import abc import typing class ShoppingCart(abc.ABC): @abc.abstractmethod def add_item(self, product_code: str, quantity: int): pass @abc.abstractmethod def print_receipt(self) -> typing.List[str]: pass import typing from . import abc class ShoppingCart(abc.ShoppingCart): def __init__(self): self._items = dict() def add_item(self, product_code: str, quantity: int): if product_code not in self._items: self._items[product_code] = quantity else: q = self._items[product_code] self._items[product_code] = q + quantity def print_receipt(self) -> typing.List[str]: lines = [] for item in self._items.items(): price = self._get_product_price(item[0]) * item[1] price_string = "€%.2f" % price lines.append(item[0] + " - " + str(item[1]) + ' - ' + price_string) return lines def _get_product_price(self, product_code: str) -> float: price = 0.0 if product_code == 'apple': price = 1.0 elif product_code == 'banana': price = 1.1 elif product_code == 'kiwi': price = 3.0 return price def test_add_item(): cart = ShoppingCart() cart.add_item("apple", 1) receipt = cart.print_receipt() assert receipt[0] == "apple - 1 - €1.00" def test_add_item_with_multiple_quantity(): cart = ShoppingCart() cart.add_item("apple", 2) receipt = cart.print_receipt() assert receipt[0] == "apple - 2 - €2.00" def test_add_different_items(): cart = ShoppingCart() cart.add_item("banana", 1) cart.add_item("kiwi", 1) receipt = cart.print_receipt() assert receipt[0] == "banana - 1 - €1.10" assert receipt[1] == "kiwi - 1 - €3.00"
avatar

Python Developer

Interviewed at Blueface

3.9
Nov 14, 2021

# Shopping cart It is a partial implementation of a shopping till system, which you might find at a supermarket. This implementation was done by a Junior developer, you as a Senior Software Engineer have been requested to refactor this project. You may make any technical decisions you would like, but must not change the given abstract class (abc.ShoppingCart) which is used by the shopping till hardware and cannot be easily updated. Please treat this code as an element of a larger production system. The code is being refactored to ensure reliability and testability. Tasks requested: - Make the receipt print items in the order that they were added - Add a 'Total' line to the receipt. This should be the full price we should charge the customer - Be able to fetch product prices from an external source (json file, database ...) - Be able to display the product prices in different currencies (not only Euro). - Update the test suite to extend coverage and limit the number of tests which need changing when changes are introduced - Any other changes which improve the reliability of this code in production If you do not have enough information, make any assumptions you would like and note them down with TODO comments. Feel free to make comments that highlight completion of the tasks listed above. Please budget 3 hours to complete. We understand you may have other commitments and time constraints and do evaluate responses with the stated level of effort in mind. Please let me know (roughly) when we should expect your answers or if you need more time. We strive to respond to your effort as promptly as we can. Please don’t forget, your code should be production ready, clean and tested! import abc import typing class ShoppingCart(abc.ABC): @abc.abstractmethod def add_item(self, product_code: str, quantity: int): pass @abc.abstractmethod def print_receipt(self) -> typing.List[str]: pass import typing from . import abc class ShoppingCart(abc.ShoppingCart): def __init__(self): self._items = dict() def add_item(self, product_code: str, quantity: int): if product_code not in self._items: self._items[product_code] = quantity else: q = self._items[product_code] self._items[product_code] = q + quantity def print_receipt(self) -> typing.List[str]: lines = [] for item in self._items.items(): price = self._get_product_price(item[0]) * item[1] price_string = "€%.2f" % price lines.append(item[0] + " - " + str(item[1]) + ' - ' + price_string) return lines def _get_product_price(self, product_code: str) -> float: price = 0.0 if product_code == 'apple': price = 1.0 elif product_code == 'banana': price = 1.1 elif product_code == 'kiwi': price = 3.0 return price def test_add_item(): cart = ShoppingCart() cart.add_item("apple", 1) receipt = cart.print_receipt() assert receipt[0] == "apple - 1 - €1.00" def test_add_item_with_multiple_quantity(): cart = ShoppingCart() cart.add_item("apple", 2) receipt = cart.print_receipt() assert receipt[0] == "apple - 2 - €2.00" def test_add_different_items(): cart = ShoppingCart() cart.add_item("banana", 1) cart.add_item("kiwi", 1) receipt = cart.print_receipt() assert receipt[0] == "banana - 1 - €1.10" assert receipt[1] == "kiwi - 1 - €3.00"

Viewing 4351 - 4360 interview questions

Glassdoor has 6,401 interview questions and reports from Python developer interviews. Prepare for your interview. Get hired. Love your job.