Table of Contents

  1. Python Basics
  2. OOP Concepts
  3. Python Advanced Topics
  4. AWS
  5. Scenario-Based Questions
  6. Coding Questions
  7. System Design
  8. Flask/Django
  9. REST APIs
  10. Databases
  11. Testing
  12. Concurrency
  13. Hashing

Python Basics

Questions and Answers

Q: What is the main purpose of a constructor in OOP? A: The main purpose of a constructor in OOP is to initialize an object with initial values. It is called when an object is created and can be used to set default values or validate initial data.

Q: What is polymorphism in OOP? A: Polymorphism is the ability of an object to take many forms. It allows methods to be called on objects regardless of their specific class, as long as they implement the method.

Q: Which data types are hashable in Python? A: Immutable data types (e.g., strings, tuples) are hashable in Python.

Q: What is the role of __name__ in Flask applications? A: The __name__ parameter is a Python built-in variable that is set to the name of the current module. When passed as an argument to the Flask class constructor, it helps Flask determine where to locate resources such as templates and static files.

Q: How do you get a visitor’s IP address in Flask? A: To get the visitor IP address in Flask, you can use request.remote_addr.

Q: What is the default host port and port of Flask? A: The default local host of Flask is 127.0.0.1, and the default port is 5000.

Q: How to change the default host and port in Flask? A: To change the default host and port in Flask, modify the app.run() statement in your Flask application.

Q: What is the Flask request lifecycle? A: The lifecycle includes: request context push, before_request functions, view function execution, after_request functions, and request context pop.

Q: What is Jinja2 in the context of Flask? A: Jinja2 is the default templating engine for Flask, used to render dynamic HTML pages.

Q: What is Pydantic and how is it used in Python applications? A: Pydantic is a data validation and settings management library using Python type annotations.

Q: How do you perform data validation with Pydantic for a JSON API request? A: Parse the JSON request data into a Pydantic model instance using the .parse_obj() method.

Q: How does file uploading work in Flask? A: The process of sending binary or regular files to a server is known as file uploading. Flask makes it simple for us to upload files. All we need is an HTML form with multipart/form-data encryption enabled.

Q: What is logging in Flask? A: Flask logging provides capability and flexibility to Flask application developers. It allows developers to construct a sophisticated, event-logging system for Flask apps.

Q: Which databases are Flask compatible with? A: As a backend database, Flask supports SQLite and MySQL. DbAdapters are used to support various databases.

Q: Why is Flask called a Microframework? A: Flask is termed “micro” because its main feature set is small and focused on the essentials needed for web development.

Q: What is Flask-JWT? A: Flask-JWT is a Flask extension that provides JSON Web Token integration for user authentication.

Q: What HTTP methods does Python Flask provide? A: Flask provides GET, POST, PUT, DELETE, and HEAD methods.

Q: What is the use of jsonify() in Flask? A: jsonify() is one of the flask.json module’s functions. It converts data to JSON and encapsulates it in a response object with the mime-type application/JSON.

Q: Is Flask Python a MVC model? A: Flask is a small form of Python framework that acts as a MVC framework, hence it is a MVC model.

Q: Describe main features of a Flask application. A: The simplicity and user-friendly nature of Flask make it an ideal framework for the development of prototypes and proofs of concept. Flask features a minimalist design that facilitates quick setup of basic applications, route definitions, and the implementation of core features.

Q: Can you describe the concept of ‘routing’ in Flask? A: Routing in Flask refers to the mapping of URLs to functions. When a user accesses a specific URL, Flask calls the associated function to process the request and return a response.

Q: How do you manage configurations in a Flask application? A: Flask allows configurations to be managed through a variety of methods, such as environment variables, configuration files, or directly within the application code.

Q: Is Flask a framework or API? A: Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.

Q: What are the disadvantages of Flask? A: Lack of built-in features compared to other frameworks, and lack of standardization in project structures.

Q: How many requests can Flask handle? A: Flask will process one request per thread at the same time. If you have 2 processes with 4 threads each, that’s 8 concurrent requests.

Q: What stack uses Flask? A: Full Stack Python.

Q: Does Flask provide caching? A: Flask itself does not provide caching, but Flask-Caching, an extension for Flask does. Flask-Caching supports various backends, and it is even possible to develop your own caching backend.

Q: Where does Flask store data? A: In order to store data across multiple requests, Flask utilizes cryptographically-signed cookies (stored on the web browser) to store the data for a session.


OOP Concepts

Questions and Answers

Q: Which of the following statements about abstract classes is true? A: Abstract classes cannot be instantiated.

Q: Which of the following allows runtime polymorphism in OOP? A: Virtual functions.

Q: What is the difference between method overloading and method overriding? A: Method overloading occurs when a class has multiple methods with the same name but different parameters. Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class.


Python Advanced Topics

Questions and Answers

Q: What are decorators in Python? A: Decorators are a way to modify or enhance the behavior of functions or methods without permanently modifying them. They are implemented using nested functions and the @ syntax.

Q: What are the key features of OOPs? A: The key features of Object-Oriented Programming Systems (OOPs) include encapsulation, inheritance, polymorphism, and abstraction.

Q: Explain the diamond problem in multiple inheritance. A: The diamond problem occurs when a class inherits from two classes that have a common base class, leading to ambiguity in method resolution. Python resolves this using the Method Resolution Order (MRO).

Q: How does Python handle race conditions? A: Python handles race conditions through the use of locks and other synchronization mechanisms in the threading module.

Q: What is the Global Interpreter Lock (GIL) in Python? A: The GIL is a mutex that prevents multiple native threads from executing Python bytecodes at once. This means that even in a multi-threaded Python program, only one thread can execute Python code at a time.

Q: How can you implement a custom iterator in Python? A: To implement a custom iterator in Python, you need to define a class with __iter__() and __next__() methods. The __iter__() method returns the iterator object itself, and __next__() returns the next value in the sequence.


AWS

Questions and Answers

Q: What is the main purpose of a testing framework? A: The main purpose of a testing framework is to automate and manage test cases.

Q: What is the role of CORS in APIs? A: CORS (Cross-Origin Resource Sharing) restricts or allows API access from specific domains, enhancing security by preventing cross-site request forgery.


Scenario-Based Questions

Task Scheduler API Scenario

Develop a Task Scheduler API for a web application. The API will manage tasks for users, allowing them to create, read, update, and delete tasks. Each user can manage their own tasks, while an admin user has the ability to manage any task in the system. The system should also support user authentication and provide advanced features like task reminders and time tracking. Please write unit test cases as well.

Loan Management System Scenario

Develop a loan management system with the following key fields:

  • loan_id: Unique identifier for each loan.
  • customer_id: Unique identifier for each customer.
  • loan_amount: Principal loan amount.
  • interest_rate: Interest rate on the loan.
  • loan_term: Duration of the loan.
  • loan_status: Current status of the loan (active, closed, defaulted, etc.).
  • issue_date: Date when the loan was issued.

Complex Data Structure Processing Scenario

Write a Python function that can process a deeply nested and complex data structure containing product information. The data structure may include a mix of lists, dictionaries, sets, and tuples with variable depth nesting. The function should extract product names and prices, considering the following complexities:

  1. Conditional Pricing
  2. Product Dependencies
  3. Budget Allocation
  4. Discounts and Offers
  5. Data Validation
  6. Edge Cases

Reconciliation API Scenario

Write a reconciliation API which generates diff between two excel/csv files. Code should be comparing the 2 files and produce the output in below format.

Single Request Processing Blocking Queue Scenario

Develop a single request processing blocking queue which is thread safe and have 1 producer and 5 consumers. Do not use standard java/python library package and instead code grounds up.


Coding Questions

Add Two Numbers (LeetCode)

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
 
def addTwoNumbers(l1, l2):
    dummy = ListNode(0)
    current = dummy
    carry = 0
    
    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        total = val1 + val2 + carry
        carry = total // 10
        current.next = ListNode(total % 10)
        current = current.next
        
        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next
    
    return dummy.next

Move Zeros to Front and Sort

Implement a function to move all zeros to the front of a list and sort the non-zero elements in ascending order.

def move_zeros_and_sort(arr):
    # Move zeros to front
    zeros = [x for x in arr if x == 0]
    non_zeros = [x for x in arr if x != 0]
    non_zeros.sort()
    return zeros + non_zeros
 
# Example usage
arr = [0, 2, 4, 0, 5, 0, 8, 0]
print(move_zeros_and_sort(arr))  # Output: [0, 0, 0, 0, 2, 4, 5, 8]

System Design

Questions and Answers

Q: How would you design a Task Scheduler API? A: The Task Scheduler API should include the following endpoints:

  • GET /tasks: Retrieve all tasks for a user
  • POST /tasks: Create a new task
  • GET /tasks/{task_id}: Retrieve a specific task
  • PUT /tasks/{task_id}: Update a task
  • DELETE /tasks/{task_id}: Delete a task

The API should implement authentication to ensure users can only access their own tasks. Admin users should have additional privileges to manage any task.

Q: How would you design a Loan Management System? A: The Loan Management System should include:

  • Database design with appropriate tables and relationships
  • API endpoints for CRUD operations on loans
  • Business logic for calculating payments and interest
  • User authentication and authorization
  • Reporting and analytics capabilities

Flask/Django

Questions and Answers

Q: What is Flask-Migrate? A: Flask-Migrate is a Flask extension that provides database migration functionality for Flask applications.

Q: What is the difference between Flask and Django? A: Flask is a microframework that provides minimal functionality and allows developers to add components as needed. Django is a full-featured framework that includes an ORM, admin panel, and many other built-in features.

Q: How do you implement authentication in Flask? A: Use Flask-Login for session-based authentication or Flask-JWT-Extended for JWT-based authentication.

Q: How do you deploy a Flask application? A: Deploy using WSGI servers like Gunicorn or uWSGI, and optionally behind a reverse proxy like Nginx.


REST APIs

Questions and Answers

Q: What is the primary purpose of the PUT method in a REST API? A: The primary purpose of the PUT method in a REST API is to update an existing resource or create it if it does not exist.

Q: What is the difference between PUT and PATCH in a REST API? A: PATCH is for partial updates, while PUT replaces the resource entirely.

Q: Which HTTP status code indicates unauthorized access? A: The HTTP status code 401 indicates unauthorized access.


Databases

Questions and Answers

Q: What is the default file naming convention for test files in Pytest? A: Files should start with test_ or end with _test.

Q: Which method in the multiprocessing module is used to share data between processes? A: Queue and Pipe are used to share data between processes in the multiprocessing module.


Testing

Questions and Answers

Q: What is the purpose of fixture in Pytest? A: Fixtures provide reusable setup and teardown logic for tests.

Q: What is the purpose of parameterized testing in Pytest? A: Parameterized testing allows testing multiple inputs and expected outputs with a single test function.


Concurrency

Questions and Answers

Q: How can you make a thread wait for another thread to complete in Python? A: You can make a thread wait for another thread to complete using thread.join().

Q: Which Python data type uses hashing for its internal implementation? A: The dictionary data type uses hashing for its internal implementation.


Hashing

Questions and Answers

Q: Which hashing algorithm is NOT provided by the hashlib module? A: RSA is not provided by the hashlib module.

Q: What is a hash collision? A: A hash collision occurs when two different inputs produce the same hash value.