0% found this document useful (0 votes)
1 views7 pages

Python Coding Interview 3

The document provides a series of Python coding interview questions and answers, covering topics such as list operations, searching algorithms, and functional programming methods. It includes examples for calculating sums, averages, counting frequencies, finding duplicates, and file handling. Additionally, it addresses important differences between various list methods and concepts relevant for data analyst and data science interviews.

Uploaded by

hmojahid050
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)
1 views7 pages

Python Coding Interview 3

The document provides a series of Python coding interview questions and answers, covering topics such as list operations, searching algorithms, and functional programming methods. It includes examples for calculating sums, averages, counting frequencies, finding duplicates, and file handling. Additionally, it addresses important differences between various list methods and concepts relevant for data analyst and data science interviews.

Uploaded by

hmojahid050
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

PYTHON CODING INTERVIEW

PART-3 (Q21–Q35)

Q21. Write a program to calculate the sum of all elements in a list.

Answer (Method 1)

numbers = [10, 20, 30, 40, 50]

total = 0

for i in numbers:
total += i

print(total)

Output

150

Easy Method

numbers = [10,20,30,40,50]

print(sum(numbers))

Q22. Write a program to calculate the average of a list.

Answer

numbers = [10,20,30,40,50]

average = sum(numbers) / len(numbers)

print(average)

Output

30.0

Q23. Write a program to count the frequency of each element in a list.

Answer

numbers = [1,2,2,3,3,3,4]

frequency = {}

for i in numbers:

if i in frequency:

frequency[i] += 1

else:
frequency[i] = 1

print(frequency)

Output

{1:1,2:2,3:3,4:1}

Q24. Write a program to find duplicate elements in a list.

Answer

numbers = [1,2,2,3,4,4,5]

duplicates = []

for i in numbers:

if [Link](i) > 1 and i not in duplicates:

[Link](i)

print(duplicates)

Output

[2,4]

Q25. Write a program to merge two lists.

Answer

list1 = [1,2,3]

list2 = [4,5,6]

result = list1 + list2

print(result)

Output

[1,2,3,4,5,6]

Q26. Write a program to find common elements between two lists.

Answer

list1 = [1,2,3,4]

list2 = [3,4,5,6]

common = []

for i in list1:

if i in list2:

[Link](i)
print(common)

Output

[3,4]

Q27. Write a program to perform Linear Search.

Answer

numbers = [10,20,30,40,50]

target = 40

for i in numbers:

if i == target:

print("Found")

break

else:

print("Not Found")

Q28. Write a program to perform Binary Search.

Answer

numbers = [10,20,30,40,50]

target = 40

low = 0

high = len(numbers)-1

while low <= high:

mid = (low+high)//2

if numbers[mid] == target:

print("Found")

break

elif numbers[mid] < target:

low = mid+1

else:

high = mid-1

⭐ Interview Question

Q. Which search is faster?

Answer:
Binary Search is much faster than Linear Search, but it only works on a sorted list.

Q29. What is List Comprehension?

Answer

List Comprehension is a short and efficient way to create a new list in Python.

Example:

square = [x*x for x in range(1,6)]

print(square)

Output

[1,4,9,16,25]

⭐ Interview Follow-up

Normal Method

square=[]

for i in range(1,6):

[Link](i*i)

Interviewer ko dono methods pata hone chahiye.

Q30. What is map()?

Answer

map() is used to apply a function to every element of an iterable.

Example

numbers = [1,2,3,4]

result = list(map(lambda x:x*2,numbers))

print(result)

Output

[2,4,6,8]

Q31. What is filter()?

Answer

filter() is used to select elements based on a condition.


Example

numbers = [1,2,3,4,5,6]

result = list(filter(lambda x:x%2==0,numbers))

print(result)

Output

[2,4,6]

Q32. What is reduce()?

Answer

reduce() reduces an iterable to a single value by repeatedly applying a function.

Example

from functools import reduce

numbers = [1,2,3,4]

result = reduce(lambda x,y:x+y,numbers)

print(result)

Output

10

Q33. How do you read a file in Python?

Answer

file = open("[Link]","r")

print([Link]())

[Link]()

Q34. How do you write data to a file?

Answer

file = open("[Link]","w")

[Link]("Hello World")

[Link]()

Q35. How do you handle exceptions in Python?

Answer

try:
num = 10/0

except ZeroDivisionError:

print("Cannot divide by zero")

⭐ Very Important Interview Questions (Rapid Fire)

Agar interviewer fast questions poochhe, to answers concise hone chahiye.

Q36. Difference between append() and extend()?

Answer: append() adds one element. extend() adds all elements from another iterable.

Q37. Difference between remove() and pop()?

Answer: remove() deletes by value. pop() deletes by index and returns the removed element.

Q38. Difference between sort() and sorted()?

Answer: sort() modifies the original list. sorted() returns a new sorted list.

Q39. Difference between deep copy and shallow copy?

Answer:

 Shallow copy: Copies only the outer object; nested objects are shared.
 Deep copy: Creates completely independent copies, including nested objects.

Q40. Difference between None and 0?

Answer:

 None means no value or absence of a value.


 0 is a valid integer value.

⭐ Python Interview Status

Ab tak humne cover kiya:

✅ Python Theory: 40+ Questions

✅ Python Coding: 35 Programs

Ye foundation fresher Data Analyst/Data Science interviews ke liye kaafi strong hai.

You might also like