Skip to content

Ananthakrishnan12/Python-Interview-prepration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Python Coding Interview Questions for Beginners

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.

πŸ“Œ Topics Covered

  • 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

πŸš€ Python Coding Questions

πŸ”Ή String & List Operations

  1. Reverse a string.
  2. Reverse a string in a list. (Input: ["h","e","l","l","o"])
  3. Reverse an array. (Input: [1,2,3,4,5])
  4. Check if a string is a palindrome. (Leetcode Q: 125)
  5. Check if a number is a palindrome. (Input: 12321)
  6. Count vowels in a string. (Input: "Hello,World!")

πŸ”Ή Recursion & Factorial

  1. Find the factorial of a number using recursion.
  2. Generate the Fibonacci sequence.

πŸ”Ή Searching & Sorting

  1. Find the maximum element in a list.
  2. Check if two strings are anagrams. (Leetcode Q: 242)
  3. Check if a number is prime.

πŸ”Ή Leetcode Problems

  1. Two Sum (Leetcode Q: 1)
  2. Longest Substring Without Repeating Characters (Leetcode Q: 3)
  3. Longest Palindromic Substring (Leetcode Q: 5)
  4. Top K Frequent Elements (Leetcode Q: 347)
  5. Merge Two Sorted Lists (Leetcode Q: 21)
  6. Reverse Integer (Leetcode Q: 7)
  7. Reverse Linked List (Leetcode Q: 206)
  8. Reverse Words in a String (Leetcode Q: 151)
  9. Missing Number (Leetcode Q: 268)
  10. First Non-Repeating Character in a String.
  11. Move All Zeroes to the End (Leetcode Q: 283)
  12. Longest Common Prefix (Leetcode Q: 14)

πŸ”Ή Additional Python Problems

  1. Count the frequency of words in a string.
  2. Find the second largest number in a list.
  3. Find all pairs in a list that sum to a target value.
  4. Remove duplicates from a string.
  5. Check if a number is an Armstrong number.
  6. Convert a string to title case.
  7. Swap two elements in a list.

πŸ”₯ List Comprehensions

  1. Generate a list of squares of even numbers from 1 to 20.
  2. Convert a list of words to uppercase if they have more than 3 letters.
  3. Create a dictionary with numbers as keys and their cubes as values.
  4. Flatten a nested list using list comprehension.

πŸ”₯ Lambda Functions

  1. Sort a list of tuples based on the second value.
  2. Use lambda to create a function that multiplies two numbers.

πŸ”₯ Functional Programming

Filter()

  1. Filter out even numbers from a list.
  2. Filter words that start with 'a'.

Map()

  1. Convert a list of strings into their lengths.

Zip()

  1. Merge two lists into a dictionary.
  2. Add corresponding elements of two lists.
  3. Find common elements in two lists.
  4. Generate a list of squares for odd numbers using list comprehension and map().

πŸ“ Pattern Matching Using Regex

  1. Count occurrences of a pattern like "code" in a string.
  2. Check if a string contains the pattern "bob" where the middle character can be anything.
  3. Double every character in a string.
  4. Transform a string by tripling vowels and doubling consonants.
  5. Extract specific words from a large string.
  6. Implement Run-Length Encoding (String Compression).

πŸ“Š Data Analysis with Pandas

πŸ›  Data Filtering

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)

Easy

  1. Filter people older than 30.
  2. Filter people living in New York.
  3. Display people with a salary greater than 60000.

Medium

  1. Filter people who live in London and are older than 30.
  2. Filter people living in New York or Paris.
  3. Filter names starting with 'A' or 'B'.
  4. Filter people aged between 25 and 35.

Hard

  1. Show people whose salary is within one standard deviation of the mean.
  2. Show people who have the same age as at least one other person.
  3. Show people with the highest salary within their city.

πŸ† String Methods & Aggregation

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)

Easy

  1. Extract the domain name from Email.
  2. Convert Name column to lowercase.
  3. Check if an email contains the word example.

Medium

  1. Extract first names from the Name column.
  2. Count occurrences of 'a' in addresses.
  3. Replace spaces in Address with underscores.
  4. Create a new column with the length of each email address.

Hard

  1. Extract usernames from Email, handling underscores.
  2. Find the longest name in the Name column.
  3. Create initials for each name.

πŸ“Š Data Manipulation

Dataset:

data_manipulation = {
    'ID': [1, 2, 3],
    'Value1': [10, 20, 30],
    'Value2': [100, 90, 80]
}
df = pd.DataFrame(data_manipulation)

Easy

  1. Create a new column Total = Value1 + Value2.
  2. Rename ID to RecordID.
  3. Delete the Value2 column.

Medium

  1. Create a Difference column = abs(Value1 - Value2).
  2. Shift Value1 column down by one row.
  3. Create a column for cumulative sum of Value1.
  4. Sort DataFrame by Value1 in descending order.

Hard

  1. Pivot the DataFrame with ID as index.
  2. Create a rolling mean column for Value1 with a window of 3.
  3. Apply a custom function: double even values, triple odd values.

Data Aggregation

Dataset

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)

Easy Tasks

  1. Calculate the mean of the 'Value' column.
  2. Count the number of rows for each category.
  3. Find the maximum value for each category.

Medium Tasks

  1. Calculate the sum of 'Value' for each 'Category' and 'Group' combination.
  2. Find the median of 'Value' for each 'Category'.
  3. Find the standard deviation of 'Value' for each 'Group'.
  4. Find the minimum value within each group.

Hard Tasks

  1. Calculate the percentage of each category's total value relative to the overall total value.
  2. Find the category with the highest average 'Value'.
  3. Calculate the rolling average of the 'Value' column grouped by category.

Data Integration

Datasets

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)

Easy Tasks

  1. Merge df_integration1 and df_integration2 based on the 'ID' column using an inner join.
  2. Concatenate df_integration1 and df_integration2 vertically (stack them on top of each other).
  3. Merge df_integration1 and df_integration2 using a left join.

Medium Tasks

  1. Merge df_integration1 and df_integration2 using an outer join and fill missing values with 0.
  2. Merge the DataFrames based on 'ID', but only include the 'Price' and 'Quantity' columns in the result.
  3. Merge the DataFrames and create a new column 'Total Cost' which is Price * Quantity.
  4. Merge the DataFrames using the right join.

Hard Tasks

  1. Merge the DataFrames and handle duplicate column names by renaming them.
  2. Perform a merge with a custom condition (e.g., merge rows where 'Price' is less than 2).
  3. Merge the DataFrames and calculate the average quantity for each product.

Statistics

Dataset

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)

Easy Tasks

  1. Calculate the mean and median of the 'Values' column.
  2. Find the standard deviation of the 'Values' column.
  3. Find the maximum and minimum of the 'Values' column.

Medium Tasks

  1. Calculate the variance of the 'Values' column.
  2. Calculate the interquartile range (IQR) of the 'Values' column.
  3. Calculate the correlation between 'Values' and a new column created by squaring 'Values'.
  4. Calculate the skewness of the β€˜Values’ column.

Hard Tasks

  1. Calculate the z-scores for each value in the 'Values' column.
  2. Perform a t-test to compare the mean of 'Values' for group 'A' versus group 'B'.
  3. Calculate the covariance between the 'Values' column and a new column that represents the cumulative sum of the 'Values' column.

πŸ’‘ Resources

Happy Coding! πŸš€

About

coding Questions for Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages