Coding problems
Strings
Reversing, comparing and rebuilding text.
Learn: Strings: characters, builders, splitting and two positions8 minute read
A string is a sequence of characters, and string questions are the most common coding questions in tester interviews: reverse a string, check a palindrome (a string that reads the same forwards and backwards), count characters, reverse the words, compress repeated letters. Interviewers ask them because testers handle text all day (input fields, log lines, API responses), and because they are easy to start but easy to get slightly wrong. Most of them need four ideas.
1. Strings cannot be changed in place
In both Java and Python a string is immutable: once created, its characters never change. Every operation that seems to change a string, such as joining two strings, creates a new one. If you build a long result by adding one character at a time to a string, each step may copy everything built so far, and the total work grows with the square of the length.
So build results in a growable buffer and turn it into a string once at the end. In Java that buffer is a StringBuilder; in Python it is a list of pieces joined with "".join(parts). To swap characters, as in a reverse, copy the string into a character array (Java) or a list of characters (Python), work on that, and convert back.
2. Characters are small numbers
Each character has a numeric code. In ASCII (the standard table that gives each basic English character a number), the digits 0 to 9 are codes 48 to 57, upper-case A to Z are 65 to 90, and lower-case a to z are 97 to 122. That gives you two tools. You can test a range: a character is a lower-case letter when it lies between a and z. And you can use the code as an index into a table of 128 counts, which is faster and smaller than a map (a collection of key-value pairs, explained in the hashing topic) for plain ASCII input. In Java a char turns into its code in arithmetic; in Python use ord() and chr(). Upper and lower case are different characters, so when a question says "ignore case", convert both sides to lower case before comparing.
3. Splitting has traps
Splitting a sentence into words looks trivial, but the two languages behave differently when spaces repeat. The program below shows it, together with a reverse built in a buffer and the character code trick.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String text = " a b ";
System.out.println(Arrays.toString(text.split(" ")));
System.out.println(Arrays.toString(text.trim().split(" +")));
StringBuilder reversed = new StringBuilder();
String word = "tester";
for (int i = word.length() - 1; i >= 0; i--) {
reversed.append(word.charAt(i));
}
System.out.println(reversed);
System.out.println((int) 'a' + " " + (char) ('a' + 2));
}
}
[, a, , b] [a, b] retset 97 c
text = " a b "
print(text.split(" "))
print(text.split())
parts = []
word = "tester"
for i in range(len(word) - 1, -1, -1):
parts.append(word[i])
print("".join(parts))
print(ord("a"), chr(ord("a") + 2))
['', 'a', '', 'b', ''] ['a', 'b'] retset 97 c
Splitting on a single space gives empty pieces wherever spaces repeat and at the edges. Java drops the empty pieces at the end but keeps those at the start and in the middle; Python keeps all of them. Python's split() with no argument splits on runs of whitespace (spaces, tabs and line breaks) and drops the empty pieces; in Java, trim the text and split on the pattern " +". Interviewers often ask you not to split at all, so be ready to walk the characters and find each word yourself.
4. Two positions
Put one index at the start and one at the end and move them toward each other. For "level", compare l with l, then e with e; the indexes meet at v, so it is a palindrome. The same walk reverses a string in place, and handles "reverse but keep the spaces where they are" by skipping a space on either side instead of swapping it. A second form moves two indexes in the same direction: one marks the start of a run of equal letters and the other finds its end, which is how "aaabb" becomes "a3b2".
Counting questions ("count each character", "first character that does not repeat", "are these anagrams", meaning two strings made of the same letters in another order) start by counting characters in a map or a table of 128 counts; the hashing topic covers them.
How to answer in an interview
First pin down the rules: does case matter, do spaces count, can the string be empty? Mention the built-in shortcut if one exists, such as a reverse function or a regular expression (a short pattern that matches text), then write the loop the interviewer asked for. Trace one short example out loud, and state the cost: one pass is O(n) time, plus O(n) space when you build a new string.
Common mistakes
- Building a long result with repeated string joining inside a loop.
- Forgetting the case rule, or comparing a space when it should be skipped.
- Splitting on a single space and getting empty words from repeated spaces.
- Reading past the end of a short string, for example when comparing prefixes.
- Turning a very long digit string into a built-in integer; add it digit by digit, as in school.
Recap
Build results in a buffer, treat characters as small numbers, split with care, and use two positions for anything that compares the two ends. Then test the empty string, one character, only spaces and very long input.
17 questions0/17 solved
The first 2 questions of each topic are free. Pro+ opens every question.