Practice: Array & String Algorithms
Practice: Array & String Algorithms
You have reached the final lesson of this tutorial. Everything you have learned — methods, loops, arrays, and strings — comes together here. Rather than introducing new syntax, this lesson is about thinking algorithmically: reading a problem, choosing the right data structure, and writing clean, correct Java that a teammate can understand.
We will work through four classic problems that appear in interviews, coding challenges, and real software all the time:
- Reverse an array in place
- Find the maximum value in an array
- Count occurrences of a character in a string
- Check whether a string is a palindrome
1. Reverse an Array In Place
Reversing in place means we do not create a second array; we swap elements within the original one. The technique is called the two-pointer approach: one pointer starts at index 0 and another at the last index. They march toward each other, swapping as they go, and stop when they meet.
temp), which is constant — O(1) space. For large arrays this matters.
2. Find the Maximum Value
The standard pattern: assume the first element is the maximum, then walk the rest of the array updating your assumption whenever you find something larger.
arr[0] on an empty array, Java throws an ArrayIndexOutOfBoundsException at runtime. A quick length check with a meaningful exception message is much easier to debug than a cryptic stack trace.
3. Count Occurrences of a Character
The String class stores characters in order. To count how many times a given character appears, loop over every position with charAt(i) and increment a counter when there is a match.
'A' and 'a' are different characters in Java. If you want a case-insensitive count, convert both the string and the target to the same case before comparing: text.toLowerCase().charAt(i) and Character.toLowerCase(target).
4. Check Whether a String Is a Palindrome
A palindrome reads the same forwards and backwards — "racecar", "level", "madam". The cleanest approach mirrors the array-reverse technique: compare the character at position left with the character at position right. If every pair matches until the pointers meet, it is a palindrome.
replaceAll with a regex once at the start, the comparison loop stays simple and focused.
Putting It All Together
Notice the shared patterns across all four algorithms:
- Two pointers — used in both the reverse and palindrome problems. Whenever you need to compare or swap from both ends of a sequence, reach for this pattern.
- Running result — used in the max and count problems. Initialise a variable before the loop, update it inside, return it after.
- Guard clauses — check edge cases (empty input, null) at the very top of the method. Let the happy path run without defensive clutter.
- Descriptive method names —
isPalindromeandfindMaxtell a reader what the method does before they read a single line of its body.
Summary
In this lesson you implemented four foundational algorithms entirely from scratch: reversing an array with the two-pointer swap, finding a maximum with a running variable, counting characters with a simple scan, and detecting palindromes with a two-pointer comparison. These patterns recur throughout computer science. Understand them well and you will recognise them — sometimes in disguise — in far more complex problems ahead.