🚀 Coding Interview Questions with Answers (Part 19)
1️⃣8️⃣1️⃣ How Do You Reverse a String?
Answer:
Reversing a string means arranging its characters in the opposite order.
Example:
Input: "hello"
Output: "olleh"
Python:
text = "hello"
reversed_text = text[::-1]
print(reversed_text)
Time Complexity: O(n)
Space Complexity: O(n)
1️⃣8️⃣2️⃣ How Do You Find the Largest Element in an Array?
Answer:
Traverse the array while keeping track of the largest value found so far.
Example:
Input: [10, 25, 7, 42, 18]
Output: 42
Python:
numbers = [10, 25, 7, 42, 18]
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print(largest)
Time Complexity: O(n)
Space Complexity: O(1)
1️⃣8️⃣3️⃣ How Do You Find the Second Largest Element in an Array?
Answer:
Maintain two variables: one for the largest element and another for the second largest. Update them while traversing the array.
Example:
Input: [10, 25, 7, 42, 18]
Output: 25
Python:
numbers = [10, 25, 7, 42, 18]
largest = second = float('-inf')
for num in numbers:
if num > largest:
second = largest
largest = num
elif largest > num > second:
second = num
print(second)
Time Complexity: O(n)
Space Complexity: O(1)
1️⃣8️⃣4️⃣ How Do You Check Whether a String is a Palindrome?
Answer:
A palindrome is a string that reads the same forward and backward.
Examples:
"madam" → Palindrome
"level" → Palindrome
"hello" → Not a palindrome
Python:
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
Time Complexity: O(n)
1️⃣8️⃣5️⃣ How Do You Find Duplicate Elements in an Array?
Answer:
Use a set to keep track of elements that have already appeared. If an element is already present in the set, it is a duplicate.
Example:
Input: [1, 2, 3, 2, 4, 1]
Output: [1, 2]
Python:
numbers = [1, 2, 3, 2, 4, 1]
seen = set()
duplicates = set()
for num in numbers:
if num in seen:
duplicates.add(num)
else:
seen.add(num)
print(duplicates)
Average Time Complexity: O(n)
Space Complexity: O(n)
1️⃣8️⃣6️⃣ How Do You Remove Duplicates from an Array?
Answer:
A common approach is to use a set, which stores only unique values.
Example:
Input: [1, 2, 2, 3, 3, 4]
Output: [1, 2, 3, 4]
Python:
numbers = [1, 2, 2, 3, 3, 4]
unique_numbers = list(set(numbers))
print(unique_numbers)
If the original order must be preserved:
unique_numbers = list(dict.fromkeys(numbers))
Average Time Complexity: O(n)
1️⃣8️⃣7️⃣ How Do You Find the Missing Number in an Array?
Answer:
If an array contains numbers from "1" to "n" with one number missing, calculate the expected sum and subtract the actual sum.
Example:
Input: [1, 2, 4, 5]
Output: 3
Python:
numbers = [1, 2, 4, 5]
n = 5
expected = n * (n + 1) // 2
missing = expected - sum(numbers)
print(missing)
Time Complexity: O(n)
Space Complexity: O(1)
1️⃣8️⃣8️⃣ How Do You Merge Two Sorted Arrays?
Answer:
Use two pointers to compare elements from both arrays and add the smaller element to the result.
Example:
Input:
[1, 3, 5]
[2, 4, 6]
Output:
[1, 2, 3, 4, 5, 6]
Python:
a = [1, 3, 5]
b = [2, 4, 6]
i = j = 0
result = []
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
while i < len(a):
result.append(a[i])
i += 1
while j < len(b):
result.append(b[j])
j += 1
print(result)