Python Interview Questions.docx
Python Interview Questions.docx
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?
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.
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.
A valid credit card from ABCD Bank has the following characteristics:
Examples:
4253625879615786
4424424424442444
5122-2368-7954-3214
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.
Input Format
The first line contains ‘n’. The second line contains an array of integers each separated
by a space.
Constraints
Output Format
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.
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 .
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] .
Example 1:
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:
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
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
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.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
You must implement a solution with a linear runtime complexity and use only constant
extra space.
Example 1:
Example 2:
Example 3:
Constraints:
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
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.
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.
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.
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.
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.
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.
Explanation: A new column bonus is created by doubling the value in the column salary.