Skip to content
DSA arrays 5 min read

String Exercises: Practice Problems (with Solutions)

Time to practice strings. Solve each problem yourself first — write the code, trace a tiny example by hand, and reason about time and space complexity (remembering the mutability rules for your language). Only then open the answer sheet. Every View answer link jumps to a full, multi-language solution on the string solutions page.

How to practice: Set a timer (15–20 min per problem). Stuck? Re-read string manipulation before peeking. Recovering from being stuck is how the patterns stick.

Warm-ups

Q1. Reverse a string. Return the input string with its characters in reverse order. Aim for O(n) time; mind that strings are immutable in most languages.

View answer →

Q2. Check a palindrome. Return true if a string reads the same forwards and backwards. Use two pointers for O(n) time and O(1) extra space.

View answer →

Q3. Count vowels. Return how many vowels (a, e, i, o, u, case-insensitive) a string contains, in a single pass.

View answer →

Core problems

Q4. Valid anagram. Given two strings, return true if one is a rearrangement of the other. Target O(n) time. Can you do it with a single frequency count?

View answer →

Q5. First unique character. Return the index of the first non-repeating character in a string, or -1 if none exists. Aim for O(n).

View answer →

Q6. Most frequent character. Return the character that appears most often (break ties however you like). State the complexity.

View answer →

Challenge

Q7. Longest substring without repeating characters. Return the length of the longest substring that contains no repeated character. Aim for a single O(n) pass using the sliding-window idea.

View answer →


Done? Review every solution on the string answer sheet — even the ones you solved, since there’s often a cleaner approach. Then continue to linked lists.

Last updated June 25, 2026
Was this helpful?