![]() |
---|
Photo by Sebastian Herrmann on Unsplash |
🔗 Follow me on LinkedIn for daily Python interview questions & insights!
📝 Read my articles on Medium for in-depth explanations & tutorials!
💬 Got a different solution? Drop your thoughts in the discussions! Let’s learn together! 🧠🔥
➡ Prepare for the Worst: Most Frequently Asked Questions by Big Tech Companies
➡ Python Projects That Can Land You Your Dream Job
➡ Credits
Python is a versatile and widely used programming language, celebrated for its simplicity and readability. 🌟 As one of the top choices in the tech industry, Python frequently appears in technical interviews across various roles, including software development, data science, and automation.
In this guide, we’ll dive into a wide array of Python interview questions and answers, covering essential topics such as:
- 📊 Data Structures: Master lists, dictionaries, sets, and more.
- 🧮 Algorithms: Understand sorting, searching, and optimization techniques.
- 🧩 Problem-solving: Sharpen your skills with real-world coding challenges.
These questions are designed to test your grasp of Python's core features, including:
- ✂️ String Manipulation
- 📋 List Operations
- 🔁 Recursion
- ➕ And much more!
Whether you're a beginner just starting your coding journey or an experienced developer brushing up on your skills, this guide will help you prepare effectively for your next Python interview. 💪✨
Get ready to boost your confidence and showcase your Python prowess! 🚀👨💻👩💻
In this section, we cover the basics of Python, which is ideal for those just starting their programming journey. You'll explore:
- 🔤 Basic Syntax: Understand Python's simple and intuitive syntax.
- 📋 Data Types: Understand integers, strings, lists, and more.
- ➗ Basic Operations: Learn arithmetic operations, string manipulations, and list handling.
- 🔄 Control Structures: Master loops, conditionals, and basic flow control.
- 🛠️ Simple Functions: Start writing your functions to organize and reuse code.
This level builds a strong foundation, ensuring you're well-prepared to move on to more advanced topics.
Python is an interpreted, high-level, general-purpose programming language. It emphasizes code readability with its use of significant indentation. Python supports multiple programming paradigms, including structured (particularly procedural), object-oriented, and functional programming.
Python code is executed line by line at runtime. Python internally converts the source code into an intermediate form called bytecode, which is then executed by the Python virtual machine (PVM).
- Easy to learn and use
- Interpreted language
- Dynamically typed
- Extensive libraries
- Object-oriented
- Portable
PEP 8 is the Python Enhancement Proposal that provides guidelines and best practices for writing Python code. It covers various aspects such as naming conventions, code layout, and indentation.
Python uses automatic memory management and a garbage collector to handle memory. The garbage collector recycles memory when objects are no longer in use.
- Numeric types:
int
,float
,complex
- Sequence types:
list
,tuple
,range
- Text type:
str
- Set types:
set
,frozenset
- Mapping type:
dict
- Boolean type:
bool
- Binary types:
bytes
,bytearray
,memoryview
- List: Mutable, can be changed after creation.
- Tuple: Immutable, cannot be changed after creation.
Using the try-except
block:
try:
# code that may raise an exception
except SomeException as e:
# code to handle the exception
import decimal
integer = 10
print(decimal.Decimal(integer))
print(type(decimal.Decimal(integer)))
> 10
> <class 'decimal.Decimal'>
import decimal
string = '12345'
print(decimal.Decimal(string))
print(type(decimal.Decimal(string)))
> 12345
> <class 'decimal.Decimal'>
string = "Python Programming"
print(string[::-1])
> gnimmargorP nohtyP
vowel = ['a', 'e', 'i', 'o', 'u']
word = "programming"
count = 0
for character in word:
if character in vowel:
count += 1
print(count)
> 3
vowel = ['a', 'e', 'i', 'o', 'u']
word = "programming"
count = 0
for character in word:
if character not in vowel:
count += 1
print(count)
> 8
word = "python"
character = "p"
count = 0
for letter in word:
if letter == character:
count += 1
print(count)
> 1
fib = [0,1]
# Range starts from 0 by default
for i in range(5):
fib.append(fib[-1] + fib[-2])
# Converting the list of integers to string
print(', '.join(str(e) for e in fib))
> 0, 1, 1, 2, 3, 5, 8
numberList = [15, 85, 35, 89, 125]
maxNum = numberList[0]
for num in numberList:
if maxNum < num:
maxNum = num
print(maxNum)
> 125
numberList = [15, 85, 35, 89, 125, 2]
minNum = numberList[0]
for num in numberList:
if minNum > num:
minNum = num
print(minNum)
> 2
numList = [1, 2, 3, 4, 5]
midElement = int((len(numList)/2))
print(numList[midElement])
> 3
lst = ["P", "Y", "T", "H", "O", "N"]
string = ''.join(lst)
print(string)
print(type(string))
> PYTHON
> <class 'str'>
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
res_lst = []
for i in range(0, len(lst1)):
res_lst.append(lst1[i] + lst2[i])
print(res_lst)
> [5, 7, 9]
Another approach
a = [1, 2, 3]
b = [4, 5, 6]
c = [x + y for x, y in zip(a, b)]
print(c)
> [5, 7, 9]
str1 = "Listen"
str2 = "Silent"
str1 = list(str1.upper())
str2 = list(str2.upper())
str1.sort(), str2.sort()
if(str1 == str2):
print("True")
else:
print("False")
> True
str1 = "Kayak".lower()
str2 = "kayak".lower()
if(str1 == str2[::-1]):
print("True")
else:
print("False")
> True
string = "P r ogramm in g "
print(string.count(' '))
> 5
# Importing Regular Expressions Library
import re
name = 'Python is 1'
digitCount = re.sub("[^0-9]", "", name)
letterCount = re.sub("[^a-zA-Z]", "", name)
spaceCount = re.findall("[ \n]", name)
print(len(digitCount))
print(len(letterCount))
print(len(spaceCount))
> 1
> 8
> 2
# Importing Regular Expressions Library
import re
spChar = "!@#$%^&*()"
count = re.sub('[\w]+', '', spChar)
print(len(count))
> 10
import re
string = "C O D E"
spaces = re.compile(r'\s+')
result = re.sub(spaces, '', string)
print(result)
> CODE
floors = 3
h = 2*floors-1
for i in range(1, 2*floors, 2):
print('{:^{}}'.format('*'*i, h))
> *
***
*****
from random import shuffle
lst = ['Python', 'is', 'Easy']
shuffle(lst)
print(lst)
> ['Easy', 'is', 'Python']
def find_largest_element(lst):
return max(lst)
# Example usage:
print(find_largest_element([1, 2, 3, 4, 5]))
> 5
def remove_duplicates(lst):
return list(set(lst))
# Example usage:
print(remove_duplicates([1, 2, 2, 3, 4, 4, 5]))
> [1, 2, 3, 4, 5]
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
# Example usage:
print(factorial(5))
> 120
def merge_sorted_lists(lst1, lst2):
return sorted(lst1 + lst2)
# Example usage:
print(merge_sorted_lists([1, 3, 5], [2, 4, 6]))
> [1, 2, 3, 4, 5, 6]
def first_non_repeating_character(s):
for i in s:
if s.count(i) == 1:
return i
return None
# Example usage:
print(first_non_repeating_character("swiss"))
> w
In this section, we dive into more complex Python topics, aimed at those looking to deepen their expertise. You’ll tackle:
- 🧠 Advanced Data Structures: Work with sets, tuples, and more complex data types.
- 🔄 Recursion and Iteration: Solve problems using advanced looping and recursive techniques.
- 🔍 Algorithms: Explore sorting, searching, and optimization algorithms in depth.
- 📦 Modules and Libraries: Leverage powerful Python libraries for various applications.
- 🛡️ Error Handling and Debugging: Learn to write robust code with effective error handling and debugging techniques.
- 🕸️ Web Scraping and APIs: Gain experience in extracting data from websites and interacting with APIs.
- 🕹️ Advanced Object-Oriented Programming (OOP): Master OOP concepts like inheritance, polymorphism, and design patterns.
This level will challenge you to think critically and apply your knowledge to complex problems, preparing you for high-level Python applications and interviews.
Metaclasses are classes of classes that define how classes behave. A class is an instance of a metaclass. They allow customization of class creation.
is
: Checks if two references point to the same object.
==
: Checks if the values of two objects are equal.
Python uses reference counting and garbage collection. Objects with a reference count of zero are automatically cleaned up by the garbage collector.
The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks in so-called context managers.
with open('file.txt', 'r') as file:
data = file.read()
@staticmethod
: Defines a method that does not operate on an instance or class; no access to self or cls.
@classmethod
: Defines a method that operates on the class itself; it receives the class as an implicit first argument (cls).
Using a metaclass
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class MyClass(metaclass=Singleton):
pass
Python uses a garbage collection mechanism based on reference counting and a cyclic garbage collector to detect and collect cycles (groups of objects that reference each other but are not accessible from any other object).
Magic methods (or dunder methods) are special methods with double underscores at the beginning and end. They enable the customization of behavior for standard operations
__init__
: Constructor__str__
: String representation__add__
: Addition operator
Using the threading
module:
import threading
def print_numbers():
for i in range(10):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
Coroutines are a type of function that can pause and resume their execution. They are defined with async def
and use await
to yield control back to the event loop.
import asyncio
async def say_hello():
await asyncio.sleep(1)
print("Hello")
asyncio.run(say_hello())
The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes simultaneously in CPython. This lock simplifies memory management and ensures thread safety, but it limits the performance of multi-threaded Python programs by allowing only one thread to execute at a time. As a result, Python multithreading is more suitable for I/O-bound tasks than CPU-bound tasks. Multiprocessing or other Python implementations, like Jython or IronPython, may be preferred for CPU-bound tasks.
A metaclass in Python is a class of a class that defines how a class behaves. Classes themselves are instances of metaclasses. You can customize class creation by defining a metaclass, such as modifying class properties, adding methods, or implementing design patterns. A common use case for metaclasses is enforcing coding standards or design patterns, such as singleton, or auto-registering classes.
The copy
module in Python provides two methods: copy()
and deepcopy()
.
-
copy.copy()
creates a shallow copy of an object. It copies the object's structure but not the elements themselves, meaning it only copies references for mutable objects. -
copy.deepcopy()
creates a deep copy of the object, including recursively copying all objects contained within the original object. Changes made to the deep-copied object do not affect the original object.
__slots__
is a special attribute in Python that allows you to explicitly declare data members (slots) and prevent the creation of __dict__
, thereby reducing memory overhead. By using __slots__
, you can limit the attributes of a class to a fixed set of fields and reduce the per-instance memory consumption. This is particularly beneficial when creating a large number of instances of a class.
-
is
checks for identity, meaning it returnsTrue
if two references point to the same object in memory. -
==
checks for equality, meaning it returnsTrue
if the values of the objects are equal, even if they are different objects in memory.
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, because both a and b refer to the same object
print(a is c) # False, because a and c refer to different objects
print(a == c) # True, because a and c have the same values
Input: [100, 4, 200, 1, 3, 2]
Output: 4 # (Sequence: [1, 2, 3, 4])
def longest_consecutive(nums):
num_set = set(nums) # Convert list to set for O(1) lookups
longest = 0
for num in num_set:
if num - 1 not in num_set: # Start of a new sequence
length = 1
while num + length in num_set:
length += 1
longest = max(longest, length)
return longest
# Example usage:
print(longest_consecutive([100, 4, 200, 1, 3, 2])) # Output: 4
Complexity:
O(N)
, whereN
is the number of elements.
As we gear up for technical interviews, it’s essential to be prepared for the toughest Python questions. If you're preparing for high-level technical interviews, you'll want to master these complex Python problems. Here’s a list of coding questions that’s been asked by top-tier companies!
Given a string, find the length of the longest substring without repeating characters.
def length_of_longest_substring(s: str) -> int:
char_set = set()
left = 0
max_len = 0
for right in range(len(s)):
while s[right] in char_set:
char_set.remove(s[left])
left += 1
char_set.add(s[right])
max_len = max(max_len, right - left + 1)
return max_len
# Example usage:
input_str = "abcabcbb"
print(length_of_longest_substring(input_str)) # Output: 3
This approach uses a sliding window and a set to keep track of characters. The left pointer moves when a duplicate character is found, ensuring all characters in the window are unique.
Asked by: Facebook
Given an unsorted array, find the Kth smallest element in the array.
import heapq
def kth_smallest(nums, k):
return heapq.nsmallest(k, nums)[-1]
# Example usage:
nums = [3, 2, 1, 5, 6, 4]
k = 2
print(kth_smallest(nums, k)) # Output: 2
Using Python's heapq.nsmallest()
function efficiently finds the Kth smallest element by retrieving the first K elements from the list and then returning the last element of that list.
Asked by: Apple
Given a collection of intervals, merge all overlapping intervals.
Answer:
def merge_intervals(intervals):
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
# Example usage:
intervals = [[1,3],[2,6],[8,10],[15,18]]
print(merge_intervals(intervals)) # Output: [[1, 6], [8, 10], [15, 18]]
First, we sort intervals by the starting point. Then, we iterate through and merge intervals if they overlap.
Asked by: Microsoft
Given a string, find the number of distinct substrings in the string.
def count_distinct_substrings(s: str) -> int:
n = len(s)
substrings = set()
for i in range(n):
for j in range(i+1, n+1):
substrings.add(s[i:j])
return len(substrings)
# Example usage:
input_str = "abc"
print(count_distinct_substrings(input_str)) # Output: 6
This solution generates all possible substrings and stores them in a set, ensuring uniqueness.
Asked by: Netflix
If you use Visual Studio Code, don't forget to read about these mind-blowing free extensions.
- Comparing Excel to PDF Conversion Methods with Python Hacks
- Unleashing Dog Breeds with Python for Fun and Laughs
- Ditch the Paper, Embrace Python: Build a Note-Taking App in 2025
- Cracking Transit Data — Calgary 2025
- Webscraping vs API
- Python’s If and Else Statements
- Pick-Up Line Generator using Python Dictionaries
- Crazy Things You Can Do With the Python Pillow Function
- The two Google Search Python Libraries you should never miss!
- Presenting Python code using RISE
- How to handle JSON in Python?
- Using the Pandas DataFrame in Day-To-Day Life
- Global, Local, and Nonlocal variables in Python
- Rendering Images inside a Pandas DataFrame
- Reading An Image In Python (Without Using Special Libraries)
- Mastering the Bar Plot in Python
- Python Operators from Scratch!!! — A Beginner’s Guide
- Splitting the dataset into three sets
- Normalization vs Standardization, which one is better
- Learning One-Hot Encoding in Python the Easy Way
- Wikipedia API for Python
- Google Trends API for Python
- Google Translate API for Python
- Speech Recognition using Python
- Learn the Python Math Module
- Learn the Python len() Function
- How to create NumPy arrays from scratch?
- Python enumerate() built-in-function
- Python Lambda Function
- Python Input, Output and Import
- Is Python object-oriented?
- Using the Pandas Data Frame as a Database.
- Python range() built-in-function
- Python eval() built-in-function
- Top Python Libraries Used In Data Science
- Predicting PewDiePie’s daily subscribers using Linear Regression
- Python Dictionary from Scratch!!!
- How to build a basic machine learning model from scratch
- Exploring the data using Python
- Python Tuples from Scratch !!!
- Python Strings from scratch !!!
- Exploratory data analysis in Python.
- Scraping two YouTube accounts using Python libraries.
- Web Scraping Using Python Libraries
- Python Pandas Data Frame Basics
- Python Lists from Scratch
- Manipulating the data with Pandas using Python
- Data Cleaning using Python with Pandas Library
This repository is created and maintained by Tanu N Prabhu. With a passion for programming and education, I aim to provide valuable resources to help others succeed in their Python journey.
Feel free to explore, contribute, and share your feedback. Happy coding! 😊👩💻👨💻
💡 Tip for interview prep: Always understand the logic behind these problems and practice writing clean, efficient code. Challenge yourself with more problems to boost your skills! 🔥