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

python_interview_questions

The document lists the top 20 Python coding interview questions along with detailed line-by-line explanations for each solution. Key topics include string manipulation, recursion, iteration, and data structure handling. Each question is accompanied by a code snippet and an explanation of its logic and functionality.

Uploaded by

Sweety Kumari
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)
11 views

python_interview_questions

The document lists the top 20 Python coding interview questions along with detailed line-by-line explanations for each solution. Key topics include string manipulation, recursion, iteration, and data structure handling. Each question is accompanied by a code snippet and an explanation of its logic and functionality.

Uploaded by

Sweety Kumari
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/ 5

Top 20 Python Coding Interview Questions with Full Line-by-Line Explanatio

1. Reverse a String Without Using [::-1]

def reverse_string(s):

result = ""

for char in s:

result = char + result

return result

Explanation:

- result = "" -> Ek empty string se start kiya.

- char + result -> Har character ko reverse order mein jodte gaye.

- Output: olleh

2. Check if a String is a Palindrome

def is_palindrome(s):

left, right = 0, len(s) - 1

while left < right:

if s[left] != s[right]:

return False

left += 1

right -= 1

return True

Explanation:

- left aur right se string ko compare karte hain.


- Agar kisi bhi point par mismatch hua -> Not a palindrome.

- Jab tak match hota raha -> True return.

3. Find the Factorial of a Number (Recursive)

def factorial(n):

if n == 0 or n == 1:

return 1

return n * factorial(n - 1)

Explanation:

- Base case: 0! and 1! = 1

- Recursive case: n * factorial(n-1) -> Call chain banta hai backward.

4. Fibonacci Sequence (Iterative)

def fibonacci(n):

a, b = 0, 1

for _ in range(n):

a, b = b, a + b

return a

Explanation:

- a, b -> Pehle do terms.

- Loop mein har baar b ko agle number ke liye shift karte hain.

- n-th Fibonacci return hota hai.

5. Find Second Largest Element in a List

def second_largest(nums):
first = second = float('-inf')

for num in nums:

if num > first:

first, second = num, first

elif first > num > second:

second = num

return second

Explanation:

- float('-inf') -> Minimum se start.

- Jab naya max mile, first and second ko shift karte hain.

- Edge case: same numbers handle karega.

6. Check for Anagram Strings

def are_anagrams(s1, s2):

return sorted(s1) == sorted(s2)

Explanation:

- sorted(s1) -> Alphabetically sort karta hai.

- Dono strings ka sorted version match hona chahiye.

7. Find All Duplicates in List

def find_duplicates(nums):

seen = set()

duplicates = set()

for num in nums:

if num in seen:
duplicates.add(num)

else:

seen.add(num)

return list(duplicates)

Explanation:

- seen set unique values track karta hai.

- Agar num dobara aata hai -> duplicates mein add.

8. Move All Zeroes to End

def move_zeroes(arr):

pos = 0

for i in range(len(arr)):

if arr[i] != 0:

arr[pos], arr[i] = arr[i], arr[pos]

pos += 1

return arr

Explanation:

- pos index non-zero ke liye.

- Swap karte hain har non-zero element ko front mein.

9. Check if Number is Prime

def is_prime(n):

if n <= 1:

return False

for i in range(2, int(n**0.5) + 1):


if n % i == 0:

return False

return True

Explanation:

- Prime number sirf 1 aur khud se divide hota hai.

- Optimized loop: up to sqrt(n) hi check karte hain.

10. Implement Binary Search

def binary_search(arr, target):

left, right = 0, len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

left = mid + 1

else:

right = mid - 1

return -1

Explanation:

- Sorted list ke liye hi kaam karta hai.

- mid element compare karte hain target se.

- Left ya right index ko shift karke search range chhoti karte hain.

(And so on with the other questions...)

You might also like