Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views8 pages

Most Asked Programming Questions

The document lists the top 20 most commonly asked programming questions in technical interviews, providing detailed explanations and Python solutions for each. Topics include string manipulation, recursion, array operations, and algorithm implementations. This resource is particularly useful for freshers and beginners preparing for coding interviews.
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 views8 pages

Most Asked Programming Questions

The document lists the top 20 most commonly asked programming questions in technical interviews, providing detailed explanations and Python solutions for each. Topics include string manipulation, recursion, array operations, and algorithm implementations. This resource is particularly useful for freshers and beginners preparing for coding interviews.
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/ 8

🚀

Top 20 Most Asked Programming Questions (With Detailed Explanation &


Solutions)

These are the most commonly asked coding questions in technical interviews, categorized for
freshers and beginners.

1. Reverse a String

🔹 Problem: Write a function to reverse a given string.​


💡 Explanation: Strings can be reversed using slicing in Python.​
✅ Solution (Python):
Python
CopyEdit
def reverse_string(s):
return s[::-1]

print(reverse_string("hello")) # Output: "olleh"

2. Check If a String is a Palindrome

🔹 Problem: Determine if a string reads the same forward and backward.​


💡 Explanation: Compare the string with its reverse.​
✅ Solution (Python):
Python
CopyEdit
def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("madam")) # Output: True


print(is_palindrome("hello")) # Output: False

3. Find the Factorial of a Number


🔹 Problem: Compute the factorial of a number using recursion.​
💡 Explanation: Factorial is defined as n! = n * (n-1)!.​
✅ Solution (Python):
Python
CopyEdit
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

4. Fibonacci Series (Iterative & Recursive)

🔹 Problem: Generate the Fibonacci sequence up to n numbers.​


💡 Explanation: Each term is the sum of the previous two terms.​
✅ Solution (Python - Iterative):
Python
CopyEdit
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

fibonacci(10) # Output: 0 1 1 2 3 5 8 13 21 34

5. Check for Prime Number

🔹 Problem: Determine if a number is prime.​


💡 Explanation: A prime number is divisible only by 1 and itself.​
✅ Solution (Python):
Python
CopyEdit
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

print(is_prime(11)) # Output: True

6. Find the Largest Element in an Array

🔹 Problem: Find the maximum value in a list.​


💡 Explanation: Use the max() function or iterate through the list.​
✅ Solution (Python):
Python
CopyEdit
def find_max(arr):
return max(arr)

print(find_max([3, 5, 7, 2, 8])) # Output: 8

7. Find the Second Largest Element in an Array

🔹 Problem: Find the second largest number in a list.​


💡 Explanation: Sort the unique elements and pick the second one.​
✅ Solution (Python):
Python
CopyEdit
def second_largest(arr):
unique_sorted = sorted(set(arr), reverse=True)
return unique_sorted[1] if len(unique_sorted) > 1 else None

print(second_largest([10, 20, 4, 45, 99])) # Output: 45


8. Find Missing Number in an Array (1 to n)

🔹 Problem: Find the missing number in a sequence from 1 to n.​


💡 Explanation: Use the sum formula for n natural numbers.​
✅ Solution (Python):
Python
CopyEdit
def missing_number(arr, n):
return n * (n + 1) // 2 - sum(arr)

print(missing_number([1, 2, 3, 5], 5)) # Output: 4

9. Find Duplicate Numbers in an Array

🔹 Problem: Identify duplicate elements in a list.​


✅ Solution (Python):
Python
CopyEdit
def find_duplicates(arr):
return list(set([x for x in arr if arr.count(x) > 1]))

print(find_duplicates([1, 2, 3, 2, 4, 5, 1])) # Output: [1, 2]

10. Check if Two Strings are Anagrams

🔹 Problem: Determine if two strings are anagrams.​


✅ Solution (Python):
Python
CopyEdit
def is_anagram(str1, str2):
return sorted(str1) == sorted(str2)

print(is_anagram("listen", "silent")) # Output: True


11. Find the Intersection of Two Lists
Python
CopyEdit
def list_intersection(list1, list2):
return list(set(list1) & set(list2))

print(list_intersection([1, 2, 3, 4], [3, 4, 5, 6])) # Output: [3, 4]

12. Remove Duplicates from a List


Python
CopyEdit
def remove_duplicates(arr):
return list(set(arr))

print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3,


4, 5]

13. Implement Binary Search


Python
CopyEdit
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

14. Find the First Non-Repeating Character in a String


Python
CopyEdit
from collections import Counter

def first_unique(s):
count = Counter(s)
for char in s:
if count[char] == 1:
return char
return None

print(first_unique("aabbcde")) # Output: "c"

15. Reverse a Linked List


Python
CopyEdit
def reverse_linked_list(head):
prev, curr = None, head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev

16. Implement a Stack using a List


Python
CopyEdit
class Stack:
def __init__(self):
self.stack = []

def push(self, val):


self.stack.append(val)

def pop(self):
return self.stack.pop() if self.stack else None

17. Check If a Linked List has a Cycle


Python
CopyEdit
def has_cycle(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

18. Find the Power of a Number


Python
CopyEdit
def power(x, y):
return x ** y

print(power(2, 3)) # Output: 8

19. Find the GCD of Two Numbers


Python
CopyEdit
import math

def gcd(a, b):


return math.gcd(a, b)

print(gcd(56, 98)) # Output: 14


20. Find the LCM of Two Numbers
Python
CopyEdit
def lcm(a, b):
return abs(a * b) // math.gcd(a, b)

print(lcm(12, 18)) # Output: 36

You might also like