DEV Community

Cover image for 4 Classic Python String and Array Interview Problems (With Simple Solutions)
Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

4 Classic Python String and Array Interview Problems (With Simple Solutions)

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"
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Create two empty lists: one for letters and another for numbers.
  2. Loop through each character in the string.
  3. Check whether the character is a letter using isalpha().
  4. If it is a letter, add it to the letters list.
  5. Otherwise, add it to the numbers list.
  6. 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)
Enter fullscreen mode Exit fullscreen mode

Output:

abc123
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Create an empty list to store unique characters.
  2. Loop through every character in the string.
  3. Check whether the character is already present in the list.
  4. If it is not present, append it.
  5. 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)
Enter fullscreen mode Exit fullscreen mode

Output:

abc
Enter fullscreen mode Exit fullscreen mode

Explanation:

Let's understand the loop:

  • First, a is not present, so it is added.
  • The next a is already present, so it is skipped.
  • The first b is added.
  • The remaining b characters are skipped.
  • The first c is added.
  • The remaining c characters 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"]
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Create a list containing the given strings.
  2. Loop through the list using the range() function.
  3. Check whether the index is odd using i % 2 != 0.
  4. If the index is odd, reverse that string using slicing.
  5. 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)
Enter fullscreen mode Exit fullscreen mode

Output:

['ABC', 'FED', 'GHI']
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Logic:

  1. Create an empty result string.
  2. Use a loop to access the string two characters at a time.
  3. The first character represents the letter.
  4. The next character represents the repetition count.
  5. Convert the digit character into an integer using int().
  6. Use another loop to repeat the letter.
  7. 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)
Enter fullscreen mode Exit fullscreen mode

Output:

abbccc
Enter fullscreen mode Exit fullscreen mode

Explanation:

Let's understand the execution:

Letter Count Repeated Result
a 1 a
b 2 bb
c 3 ccc

Final result:

abbccc
Enter fullscreen mode Exit fullscreen mode

Top comments (0)