4 Classic Python String and Array Interview Problems (With Simple Solutions)
String and array manipulation questions are commonly asked in Python technical interviews. These problems help you understand how to work with strings, loops, conditions, lists, and built-in methods.
In this blog, we will learn four important problems with simple Python solutions, step-by-step logic, and expected outputs.
1. Separate Letters and Numbers
Problem: Given an alphanumeric string, move all the letters to the beginning and all the numbers to the end while maintaining their original order.
Example:
Input: "a1b2c3"
Output: "abc123"
Logic:
- Create two empty lists: one for letters and another for numbers.
- Loop through each character in the string.
- Check whether the character is a letter using
isalpha(). - If it is a letter, add it to the letters list.
- Otherwise, add it to the numbers list.
- Join both lists to get the final result.
Python Code:
s = "a1b2c3"
letters = []
numbers = []
for ch in s:
if ch.isalpha():
letters.append(ch)
else:
numbers.append(ch)
result = "".join(letters) + "".join(numbers)
print(result)
Output:
abc123
Explanation:
-
isalpha()checks whether a character is a letter. -
append()adds a character to a list. -
"".join()combines list elements into a string.
2. Remove Duplicate Characters
Problem: Remove duplicate characters from a string while preserving the original order of the characters.
Example:
Input: "aaaaaabbbbbbbbbbcccccc"
Output: "abc"
Logic:
- Create an empty list to store unique characters.
- Loop through every character in the string.
- Check whether the character is already present in the list.
- If it is not present, append it.
- Join the list elements to create the final string.
Python Code:
s = "aaaaaabbbbbbbbbbcccccc"
unique = []
for ch in s:
if ch not in unique:
unique.append(ch)
result = "".join(unique)
print(result)
Output:
abc
Explanation:
Let's understand the loop:
- First,
ais not present, so it is added. - The next
ais already present, so it is skipped. - The first
bis added. - The remaining
bcharacters are skipped. - The first
cis added. - The remaining
ccharacters are skipped.
Finally, the result is abc.
Important: Python lists preserve insertion order, so the first occurrence of each character remains in its original position.
3. Reverse Odd-Indexed Elements in an Array
Problem: Given an array of strings, reverse the elements present at odd indices (1, 3, 5...) while keeping even-indexed elements unchanged.
Example:
Input: ["ABC", "DEF", "GHI"]
Output: ["ABC", "FED", "GHI"]
Logic:
- Create a list containing the given strings.
- Loop through the list using the
range()function. - Check whether the index is odd using
i % 2 != 0. - If the index is odd, reverse that string using slicing.
- Print the updated list.
Python Code:
arr = ["ABC", "DEF", "GHI"]
for i in range(len(arr)):
if i % 2 != 0:
arr[i] = arr[i][::-1]
print(arr)
Output:
['ABC', 'FED', 'GHI']
Explanation:
Python list indexing starts from zero.
| Index | Original Value | Action | Final Value |
|---|---|---|---|
| 0 | ABC | No change | ABC |
| 1 | DEF | Reverse | FED |
| 2 | GHI | No change | GHI |
Important concepts:
-
len(arr)returns the number of elements. -
range()generates the index values. -
%is the modulus operator. -
[::-1]reverses a string using slicing.
4. Expand Compressed Strings
Problem: Given a string where every letter is followed by a single-digit number, repeat each letter according to its number.
Example:
Input: "a1b2c3"
Output: "abbccc"
Logic:
- Create an empty result string.
- Use a loop to access the string two characters at a time.
- The first character represents the letter.
- The next character represents the repetition count.
- Convert the digit character into an integer using
int(). - Use another loop to repeat the letter.
- Add the repeated letters to the result.
Python Code:
s = "a1b2c3"
result = ""
for i in range(0, len(s), 2):
ch = s[i]
count = int(s[i + 1])
for j in range(count):
result = result + ch
print(result)
Output:
abbccc
Explanation:
Let's understand the execution:
| Letter | Count | Repeated Result |
|---|---|---|
| a | 1 | a |
| b | 2 | bb |
| c | 3 | ccc |
Final result:
abbccc
Top comments (0)