This repository contains a collection of Python coding questions that are commonly asked in interviews. These questions cover various topics such as string manipulation, list comprehensions, lambda functions, filtering, mapping, and data manipulation using Pandas.
- String Manipulation
- List Operations
- Recursion
- Searching & Sorting
- Leetcode Problems
- Lambda Functions
- Functional Programming (Filter, Map, Zip)
- Regular Expressions (Regex)
- Pandas Data Filtering & Manipulation
- String Methods & Aggregation
- Reverse a string.
- Reverse a string in a list. (Input:
["h","e","l","l","o"]
) - Reverse an array. (Input:
[1,2,3,4,5]
) - Check if a string is a palindrome. (Leetcode Q: 125)
- Check if a number is a palindrome. (Input:
12321
) - Count vowels in a string. (Input:
"Hello,World!"
)
- Find the factorial of a number using recursion.
- Generate the Fibonacci sequence.
- Find the maximum element in a list.
- Check if two strings are anagrams. (Leetcode Q: 242)
- Check if a number is prime.
- Two Sum (Leetcode Q: 1)
- Longest Substring Without Repeating Characters (Leetcode Q: 3)
- Longest Palindromic Substring (Leetcode Q: 5)
- Top K Frequent Elements (Leetcode Q: 347)
- Merge Two Sorted Lists (Leetcode Q: 21)
- Reverse Integer (Leetcode Q: 7)
- Reverse Linked List (Leetcode Q: 206)
- Reverse Words in a String (Leetcode Q: 151)
- Missing Number (Leetcode Q: 268)
- First Non-Repeating Character in a String.
- Move All Zeroes to the End (Leetcode Q: 283)
- Longest Common Prefix (Leetcode Q: 14)
- Count the frequency of words in a string.
- Find the second largest number in a list.
- Find all pairs in a list that sum to a target value.
- Remove duplicates from a string.
- Check if a number is an Armstrong number.
- Convert a string to title case.
- Swap two elements in a list.
- Generate a list of squares of even numbers from
1
to20
. - Convert a list of words to uppercase if they have more than
3
letters. - Create a dictionary with numbers as keys and their cubes as values.
- Flatten a nested list using list comprehension.
- Sort a list of tuples based on the second value.
- Use lambda to create a function that multiplies two numbers.
- Filter out even numbers from a list.
- Filter words that start with 'a'.
- Convert a list of strings into their lengths.
- Merge two lists into a dictionary.
- Add corresponding elements of two lists.
- Find common elements in two lists.
- Generate a list of squares for odd numbers using list comprehension and
map()
.
- Count occurrences of a pattern like "code" in a string.
- Check if a string contains the pattern "bob" where the middle character can be anything.
- Double every character in a string.
- Transform a string by tripling vowels and doubling consonants.
- Extract specific words from a large string.
- Implement Run-Length Encoding (String Compression).
Dataset:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 22, 35],
'City': ['New York', 'London', 'Tokyo', 'Paris'],
'Salary': [50000, 60000, 45000, 70000]
}
df = pd.DataFrame(data)
- Filter people older than 30.
- Filter people living in
New York
. - Display people with a salary greater than
60000
.
- Filter people who live in
London
and are older than30
. - Filter people living in
New York
orParis
. - Filter names starting with 'A' or 'B'.
- Filter people aged between
25
and35
.
- Show people whose salary is within one standard deviation of the mean.
- Show people who have the same age as at least one other person.
- Show people with the highest salary within their city.
Dataset:
string_data = {
'Email': ['alice@example.com', 'bob@test.co.uk'],
'Name': ['Alice Smith', 'Bob Smith'],
'Address': ['123 Main St', '456 Oak Ave']
}
df = pd.DataFrame(string_data)
- Extract the domain name from
Email
. - Convert
Name
column to lowercase. - Check if an email contains the word
example
.
- Extract first names from the
Name
column. - Count occurrences of 'a' in addresses.
- Replace spaces in
Address
with underscores. - Create a new column with the length of each email address.
- Extract usernames from
Email
, handling underscores. - Find the longest name in the
Name
column. - Create initials for each name.
Dataset:
data_manipulation = {
'ID': [1, 2, 3],
'Value1': [10, 20, 30],
'Value2': [100, 90, 80]
}
df = pd.DataFrame(data_manipulation)
- Create a new column
Total
=Value1
+Value2
. - Rename
ID
toRecordID
. - Delete the
Value2
column.
- Create a
Difference
column =abs(Value1 - Value2)
. - Shift
Value1
column down by one row. - Create a column for cumulative sum of
Value1
. - Sort DataFrame by
Value1
in descending order.
- Pivot the DataFrame with
ID
as index. - Create a rolling mean column for
Value1
with a window of3
. - Apply a custom function: double even values, triple odd values.
import pandas as pd
data_aggregation_data = {
'Category': ['A', 'B', 'A', 'B', 'C', 'A', 'C', 'B', 'A', 'C'],
'Value': [10, 20, 15, 25, 30, 12, 35, 22, 18, 40],
'Group': ['X', 'Y', 'X', 'Y', 'Z', 'X', 'Z', 'Y', 'X', 'Z']
}
df_aggregation = pd.DataFrame(data_aggregation_data)
- Calculate the mean of the 'Value' column.
- Count the number of rows for each category.
- Find the maximum value for each category.
- Calculate the sum of 'Value' for each 'Category' and 'Group' combination.
- Find the median of 'Value' for each 'Category'.
- Find the standard deviation of 'Value' for each 'Group'.
- Find the minimum value within each group.
- Calculate the percentage of each category's total value relative to the overall total value.
- Find the category with the highest average 'Value'.
- Calculate the rolling average of the 'Value' column grouped by category.
import pandas as pd
data_integration_data1 = {
'ID': [1, 2, 3, 4, 5],
'Product': ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'],
'Price': [1.0, 0.5, 2.0, 1.5, 2.5]
}
df_integration1 = pd.DataFrame(data_integration_data1)
data_integration_data2 = {
'ID': [3, 4, 5, 6, 7],
'Quantity': [10, 20, 30, 40, 50],
'Location': ['Store A', 'Store B', 'Store C', 'Store D', 'Store E']
}
df_integration2 = pd.DataFrame(data_integration_data2)
- Merge
df_integration1
anddf_integration2
based on the 'ID' column using an inner join. - Concatenate
df_integration1
anddf_integration2
vertically (stack them on top of each other). - Merge
df_integration1
anddf_integration2
using a left join.
- Merge
df_integration1
anddf_integration2
using an outer join and fill missing values with 0. - Merge the DataFrames based on 'ID', but only include the 'Price' and 'Quantity' columns in the result.
- Merge the DataFrames and create a new column 'Total Cost' which is
Price * Quantity
. - Merge the DataFrames using the right join.
- Merge the DataFrames and handle duplicate column names by renaming them.
- Perform a merge with a custom condition (e.g., merge rows where 'Price' is less than 2).
- Merge the DataFrames and calculate the average quantity for each product.
import pandas as pd
import numpy as np
statistics_data = {
'Values': [10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
'Group': ['A', 'A', 'B', 'B', 'C', 'C', 'A', 'B', 'C', 'A']
}
df_statistics = pd.DataFrame(statistics_data)
- Calculate the mean and median of the 'Values' column.
- Find the standard deviation of the 'Values' column.
- Find the maximum and minimum of the 'Values' column.
- Calculate the variance of the 'Values' column.
- Calculate the interquartile range (IQR) of the 'Values' column.
- Calculate the correlation between 'Values' and a new column created by squaring 'Values'.
- Calculate the skewness of the βValuesβ column.
- Calculate the z-scores for each value in the 'Values' column.
- Perform a t-test to compare the mean of 'Values' for group 'A' versus group 'B'.
- Calculate the covariance between the 'Values' column and a new column that represents the cumulative sum of the 'Values' column.
Happy Coding! π