0% found this document useful (0 votes)
3 views

Python Interview Questions.docx

Uploaded by

nikhilkumar84310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Interview Questions.docx

Uploaded by

nikhilkumar84310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Python Interview Questions

A.​ Basic Python:

1.​ What is Lambda Function?


2.​ When to use a List, tuple or dictionary?
3.​ What is the difference between List and tuple?
4.​ What is __init() ?
5.​ What is the difference between Mutable and Immutable data type?
6.​ What is the difference between Shallow copy and deep copy?
7.​ What are Decorators?
8.​ How can you replace string space with a given character in Python?
9.​ Write a Python program to determine if a given string is a palindrome.
10.​Write a program to swap 2 values without using a third variable.
11.​What is break, continue and pass in Python?
12.​Print every third number from 1 to 50.
13.​Write a program to check if a number is a three-digit number.
14.​Write a program to check if a string has more than 5 characters.
15.​Write a program to check if a string starts with the letter 'A'.
16.​Write a program to check if a given string is a palindrome
17.​Write a program to check if a string contains the word "Python".
18.​How are arguments passed by value or by reference in python?
19.​What is the use of help() and dir() functions?
20.​ Define OOPs concepts.
21.​What are global, protected and private attributes in Python?
22.​What is slicing in Python? Give example
23.​ What is inheritance and types of inheritance? Explain with examples.
24.​ Write a Program to find the squares of double of the numbers present in array.
25.​Write a Python program to accept a filename from the user and print the extension
of that.
Sample filename : abc.java Output : java
26.​Split a number into a ratio of 3:5.
27.​Write a program to check if a given number is Armstrong number.
(i.e 153=1*1*1+5*5*5+3*3*3)
28.​Write a program to check if a given time is AM or PM.
29.​ If a list, list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] is given. Extract
below output.
Output: python is fun

30.​ If l=[4,5,6,7,2,3,11,13], Write a program in such a way that even numbers should
come first and odd numbers at last.
31.​What is a dynamically typed language?
32.​How would you find the largest number in an unsorted list?
33.​What is the ‘with’ statement in Python?
34.​How can you optimize python code?
35.​What is the difference between == and ‘is’ in Python?
36.​Write a program to find the common elements between two lists.
37.​Write a program to find the common elements between two lists.
38.​Can we pass a function as an argument in Python?
39.​What is the difference between Python Arrays and List.
40.​What is the difference between xrange and range functions?
41.​What is Dictionary Comprehension? Give an Example
42.​What is the function of enumerate in python?
43.​What are Pickling and Unpickling?
44.​Write a code to display the current time?
45.​What are some of the most common Python libraries that are used in data science?
46.​What is the purpose of the super() function in Python?
47.​What is the Global Interpreter Lock (GIL) in Python?
48.​What is the purpose of the yield keyword in Python?
49.​How can you format a datetime object as a string in Python?
50.​What is the concept of multithreading in python?
51.​Given an integer array, find the sum of the largest contiguous subarray within the
array. For example, given the array A = [0,-1,-5,-2,3,14] it should return 17
because of [3,14]. Note that if all the elements are negative it should return zero.
52.​Define tuples and lists in Python What are the major differences between them?

B.​ Data Transformation and Visualizations:

1.​ What is the difference between Series and a dataframe?


2.​ How would you find duplicate values in a dataset for a variable in Python?
3.​ How to handle missing values?
4.​ How to Handle Outliers?
5.​ What is the difference between merge and join and concatenate?
6.​ Explain split() and join() functions in Python?
7.​ What is an array?
8.​ What is the difference between loc and iloc?
9.​ How to handle categorical data in python?
10.​How to perform one hot encoding?
11.​What is the purpose of map() in python and what is it’s relevance in data analysis?
12.​What is the purpose of merge() and join()?
13.​Write a python code to identify duplicate entries in a dataset?
14.​Write Python code to calculate the correlation between two variables in a dataset.
15.​How would you transform categorical data for a machine learning model?
16.​What is the difference between supervised and unsupervised learning?
17.​In a given dataset, how will you summarize Total sales by city.
18.​You are working with a dataset of sales transactions. How to find the total sales
for each product in each region.
19.​You have customer data and you need to show how many customers bought
different combinations of products. How will you do this?
20.​You have sales data for multiple regions. How will you calculate both the total
sales and average sales for each region?
21.​How do you apply a function to all elements in any DataFrame column?
22.​What is the purpose of apply() in Pandas?
23.​How to convert dataframe into excel file?
24.​How can we convert Series to DataFrame?
25.​How to reset the index in a Python Pandas DataFrame?
26.​Compare the Pandas methods: map(), applymap(), apply()
27.​How to find Bottom 5 stores on the basis of sales in a dataframe?
28.​You have been given a dataframe, how would you filter data where age is greater
than 30 and name starts with "C"?
29.​What is the purpose of the join() method in Python?
30.​How do we calculate basic statistics like mean, median, etc. for a given
dataframe?
31.​What is broadcasting in NumPy?
32.​How does Seaborn differ from Matplotlib?
33.​Explain how Python handles type conversions.
34.​How to handle columns with inappropriate data types. Also how would you slice
a DataFrame using a date range?
35.​How can you filter rows in a DataFrame where values of one column are greater
than the mean of that column?
36.​What is the use of scatter plots in data visualization?
37.​What are some common ways to handle missing data in Python, and which
method do you prefer and why?
38.​When is it best to remove missing values?
39.​How would you analyze and handle outliers in a dataset?
Ans:
a.​ Visual methods: Outliers can be visually identified using charts like
boxplots and scatterplots Points that are outside the whiskers of a boxplot
are typically outliers. When using scatterplots, outliers can be detected as
points that are far away from other data points in the visualization.
b.​ Non-visual methods: One non-visual technique to detect outliers is the
Z-Score. Z-Scores are computed by subtracting a value from the mean and
dividing it by the standard deviation. This tells us how many standard
deviations away from the mean a value is. Values that are above or below
3 standard deviations from the mean are considered outliers.
40.​What is the purpose of the pd.melt() function in Pandas?
C.​ Statistical Analysis:

1.​ What is the difference between descriptive and inferential statistics?


2.​ What are different sampling methods in python?
3.​ How can we perform hypothesis testing in Python?
4.​ How can you check if a dataset is normally distributed or not?
5.​ What is multicollinearity, and how can we detect it ?
6.​ What is skewness?
7.​ What is a p-value?
8.​ What are Type I and Type II errors in hypothesis testing?
9.​ What is regression?
10.​What are residuals?
11.​What is a confidence interval?
12.​Why do we do a train/test split?
13.​Can you explain overfitting and underfitting?
14.​Describe the statistical significance of hypothesis testing.
15.​ Explain what a KPI is in statistical terms.
16.​Is a large data set always best?
17.​Can you explain the difference between a sample and a population?
18.​What is the difference between a parametric and non-parametric test?
19.​What is the difference between covariance and correlation?
20.​Differentiate between a one-tailed and two-tailed test.
21.​Given the following scenario, which statistical test would you choose to
implement?
An online retailer wants to evaluate the effectiveness of a new ad campaign. They
collect daily sales data for 30 days before and after the ad was launched. The
company wants to determine if the ad contributed to a significant difference in
daily sales.
Ans:
To evaluate the effectiveness of a new ad campaign, we should use paired
t-test. A paired t-test is used to compare the means of two samples and check if a
difference is statistically significant.
In this case, we are comparing sales before and after the ad was run, comparing a
change in the same group of data, which is why we use a paired t-test instead of
an independent sample t-test.

Scenario based questions:

1.​ Given a string s consisting of words and spaces, return the length of the last word in the
string.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.

Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

Constraints:
1 <= s.length <= 104
s consists of only English letters and spaces ' '.
There will be at least one word in s.

2.​ In this challenge, the user enters a string and a substring. You have to print the number of
times that the substring occurs in the given string. String traversal will take place from
left to right, not from right to left.

NOTE: String letters are case-sensitive.

Input Format
The first line of input contains the original string. The next line contains the substring.

Constraints
Each character in the string is an ascii character.

Output Format

Output the integer number indicating the total number of occurrences of the substring in
the original string.

Sample Input
ABCDCDC
CDC
Sample Output
2

Concept
There are a couple of new concepts:
In Python, the length of a string is found by the function len(s), where is the string.
To traverse through the length of a string, use a for loop:
for i in range(0, len(s)):
print (s[i])
A range function is used to loop over some length:

range (0, 5)
Here, the range loops over 0 to 4.5 is excluded.

3.​ Validating Credit card numbers:


You and Fredrick are good friends. Yesterday, Fredrick received N credit cards from
ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You
happen to be great at regex so he is asking for your help!

A valid credit card from ABCD Bank has the following characteristics:

► It must start with a 4, 5 or 6 .


► It must contain exactly 16 digits.
► It must only consist of digits (0-9).
► It may have digits in groups of 4, separated by one hyphen "-".
► It must NOT use any other separator like ' ' , '_', etc.
► It must NOT have 4 or more consecutive repeated digits.

Examples:

Valid Credit Card Numbers

4253625879615786
4424424424442444
5122-2368-7954-3214

Invalid Credit Card Numbers

42536258796157867 #17 digits in card number → Invalid


4424444424442444 #Consecutive digits are repeating 4 or more times → Invalid
5122-2368-7954 - 3214 #Separators other than '-' are used → Invalid
44244x4424442444 #Contains non digit characters → Invalid
0525362587961578 #Doesn't start with 4, 5 or 6 → Invalid

Input Format
The first line of input contains an integer N.
The next N lines contain credit card numbers.

Constraints : 0<N<100

Output Format
Print 'Valid' if the credit card number is valid. Otherwise, print 'Invalid'. Do not print the
quotes.

Sample Input

6
4123456789123456
5123-4567-8912-3456
61234-567-8912-3456
4123356789123456
5133-3367-8912-3456
5123 - 3567 - 8912 - 3456

Sample Output

Valid
Valid
Invalid
Valid
Invalid
Invalid

Explanation

4123456789123456 : Valid
5123-4567-8912-3456 : Valid
61234--8912-3456 : Invalid, because the card number is not divided into equal groups of
4.
4123356789123456 : Valid
51-67-8912-3456 : Invalid, consecutive digits 3333 is repeated 4 times.
5123 - 4567 - 8912 - 3456 : Invalid, because space ' ' and - are used as separators.

4.​ Find the runner up score:


Given the participants' score sheet for your University Sports Day, you are required to
find the runner-up score. You are given scores. Store them in a list and find the score of
the runner-up.

Input Format

The first line contains ‘n’. The second line contains an array of integers each separated
by a space.

Constraints

Output Format

Print the runner-up score.

Sample Input 0

5
23665

Sample Output 0: 5

Explanation 0
The given list is [2,3,6,6,5] . The maximum score is 6, second maximum is 5 . Hence, we
print 5 as the runner-up score.

5.​ Let's learn some new Python concepts! You have to generate a list of the first N
fibonacci numbers, 0 being the first number. Then, apply the map function and a lambda
expression to cube each fibonacci number and print the list.

Concept
The map() function applies a function to every member of an iterable and returns the
result. It takes two parameters: first, the function that is to be applied and secondly, the
iterables.
Let's say you are given a list of names, and you have to print a list that contains the length
of each name.

>> print (list(map(len, ['Tina', 'Raj', 'Tom'])))


[4, 3, 3]

Lambda is a single expression anonymous function often used as an inline function. In


simple words, it is a function that has only one line in its body. It proves very handy in
functional and GUI programming.

>> sum = lambda a, b, c: a + b + c


>> sum(1, 2, 3)
6

Note:
Lambda functions cannot use the return statement and can only have a single expression.
Unlike def, which creates a function and assigns it a name, lambda creates a function and
returns the function itself. Lambda can be used inside lists and dictionaries.

Input Format
One line of input: an integer N .

Constraints: 0<= N<= 15

Output Format
A list on a single line containing the cubes of the first N fibonacci numbers.

Sample Input:5
Sample Output: [0, 1, 1, 8, 27]
Explanation
The first 5 fibonacci numbers are [0,1,1, 2,3] , and their cubes are [0,1,1,8,27] .

6.​ Finding missing number:


Given an array nums containing n distinct numbers in the range [0, n], return the only
number in the range that is missing from the array.

Example 1:

Input: nums = [3,0,1]


Output: 2

Explanation:
n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing
number in the range since it does not appear in nums.

Example 2:
Input: nums = [0,1]

Output: 2

Explanation:
n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing
number in the range since it does not appear in nums.

Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]

Output: 8

Explanation:
n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing
number in the range since it does not appear in nums.

Constraints:
n == nums.length
1 <= n <= 104
0 <= nums[i] <= n

All the numbers of nums are unique.

7.​ Climbing Stars:


You are climbing a staircase. It takes n steps to reach the top. Each time you can either
climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

Constraints: 1 <= n <= 45

8.​ Two Sum:

Given an array of integers nums and an integer target, return indices of the two numbers
such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use
the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9


Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6


Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6


Output: [0,1]

Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.

9.​ Single number:


Given a non-empty array of integers nums, every element appears twice except for one.
Find that single one.

You must implement a solution with a linear runtime complexity and use only constant
extra space.

Example 1:

Input: nums = [2,2,1]


Output: 1

Example 2:

Input: nums = [4,1,2,1,2]


Output: 4

Example 3:

Input: nums = [1]


Output: 1

Constraints:

1 <= nums.length <= 3 * 104


-3 * 104 <= nums[i] <= 3 * 104
Each element in the array appears twice except for one element which appears only once.

10.​You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and
two integers m and n, representing the number of elements
in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside
the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements
denote the elements that should be merged, and the last n elements are set to 0 and should be
ignored. nums2 has a length of n.

Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge
result can fit in nums1.

Constraints:
●​ nums1.length == m + n
●​ nums2.length == n
●​ 0 <= m, n <= 200
●​ 1 <= m + n <= 200
●​ -109 <= nums1[i], nums2[j] <= 109

11.​Given an array nums. We define a running sum of an array as :


runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Example 2:
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1,1+1+1+1+1].

Example 3:
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

Constraints:
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6

12.​You are given an array of integers nums. You are also given an integer original which is
the first number that needs to be searched for in nums.
You then do the following steps:
If original is found in nums, multiply it by two (i.e., set original = 2 * original).
Otherwise, stop the process.
Repeat this process with the new number as long as you keep finding the number.
Return the final value of original.

Example 1:
Input: nums = [5,3,6,1,12], original = 3
Output: 24
Explanation:
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.

Example 2:
Input: nums = [2,7,9], original = 4
Output: 4
Explanation:
- 4 is not found in nums. Thus, 4 is returned.
Constraints:
1 <= nums.length <= 1000
1 <= nums[i], original <= 1000

13.​Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+

id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.

Write a Python solution using UDF to find the nth highest salary from the Employee table.
If there is no nth highest salary, return null.

The result format is in the following example.


Example 1:

Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
n=2
Output:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+

Example 2:

Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
n=2
Output:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null |
+------------------------+

14.​Table: Employee

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
| salary | int |
| managerId | int |
+-------------+---------+

id is the primary key (column with unique values) for this table.
Each row of this table indicates the ID of an employee, their name, salary, and the ID of
their manager.

Write a solution to find the employees who earn more than their managers.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:
Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | Null |
| 4 | Max | 90000 | Null |
+----+-------+--------+-----------+
Output:
+----------+
| Employee |
+----------+
| Joe |
+----------+

Explanation: Joe is the only employee who earns more than his manager.

15.​Table: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
id is the primary key (column with unique values) for this table.
departmentId is a foreign key (reference columns) of the ID from the Department table.
Each row of this table indicates the ID, name, and salary of an employee. It also contains
the ID of their department.

Table: Department
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table. It is guaranteed that
department name is not NULL.
Each row of this table indicates the ID of a department and its name.

Write a solution to find employees who have the highest salary in each of the departments.
Return the result table in any order.

The result format is in the following example.

Example 1:
Input:
Employee table:
+----+-------+--------+--------------+
| id | name | salary | departmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department table:
+----+-------+
| id | name |
+----+-------+
| 1 | IT |
| 2 | Sales |
+----+-------+
Output:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
| IT | Max | 90000 |
+------------+----------+--------+
Explanation: Max and Jim both have the highest salary in the IT department and Henry has
the highest salary in the Sales department.

16.​Table: Employee

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| empId | int |
| name | varchar |
| supervisor | int |
| salary | int |
+-------------+---------+
empId is the column with unique values for this table.
Each row of this table indicates the name and the ID of an employee in addition to their
salary and the id of their manager.

Table: Bonus
+-------------+------+
| Column Name | Type |
+-------------+------+
| empId | int |
| bonus | int |
+-------------+------+
empId is the column of unique values for this table.
empId is a foreign key (reference column) to empId from the Employee table.
Each row of this table contains the id of an employee and their respective bonus.

Write a solution to report the name and bonus amount of each employee with a bonus less
than 1000.

Return the result table in any order.

The result format is in the following example.


Example 1:

Input:
Employee table:
+-------+--------+------------+--------+
| empId | name | supervisor | salary |
+-------+--------+------------+--------+
| 3 | Brad | null | 4000 |
| 1 | John | 3 | 1000 |
| 2 | Dan | 3 | 2000 |
| 4 | Thomas | 3 | 4000 |
+-------+--------+------------+--------+
Bonus table:
+-------+-------+
| empId | bonus |
+-------+-------+
| 2 | 500 |
| 4 | 2000 |
+-------+-------+
Output:
+------+-------+
| name | bonus |
+------+-------+
| Brad | null |
| John | null |
| Dan | 500 |
+------+-------+

17.​Table: Views

There is no primary key (column with unique values) for this table, the table may have
duplicate rows.
Each row of this table indicates that some viewer viewed an article (written by some author)
on some date.
Note that equal author_id and viewer_id indicate the same person.
Write a solution to find all the authors that viewed at least one of their own articles. Return
the result table sorted by id in ascending order.
The result format is in the following example.

Example 1:
Input:
Views table:
Output:

18.​Table: Users

user_id is the primary key (column with unique values) for this table.
This table contains information of the users signed up in a website. Some e-mails are invalid.
Write a solution to find the users who have valid emails.

A valid e-mail has a prefix name and a domain where:


The prefix name is a string that may contain letters (upper or lower case), digits, underscore '_',
period '.', and/or dash '-'. The prefix name must start with a letter.
The domain is '@leetcode.com'.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Users table:

Explanation:
The mail of user 2 does not have a domain.
The mail of user 5 has the # sign which is not allowed.
The mail of user 6 does not have the leetcode domain.
The mail of user 7 starts with a period.

19.​ DataFrame employees

A company plans to provide its employees with a bonus.


Write a solution to create a new column name bonus that contains the doubled values of the
salary column.
The result format is in the following example.
Example 1:
Input:

Explanation: A new column bonus is created by doubling the value in the column salary.

You might also like