This is a straightforward and basic task to implement: given a string input to a function we want to return its reverse.
There’s a wide range of solutions to this problem and here we’ll go through 3 of them using C#:
- the built-in
Reverse
extension function - the
Aggregate
LINQ operator - bare-bones solution based on a for-loop
Here are the three implementations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Algorithms { public class StringReverse { public string ReverseString(string input) { return ReverseStringWithReverse(input); //return ReverseStringWithAggregate(input); //return ReverseStringWithLoop(input); } private string ReverseStringWithReverse(string input) { var reverse = input.Reverse(); //return string.Join("", reverse); return string.Concat(reverse); //or in one line string.Concat(input.Reverse()) } private string ReverseStringWithAggregate(string input) { return input.Aggregate("", (c1, c2) => c2 + c1); } private string ReverseStringWithLoop(string input) { string reversed = ""; for (int i = input.Length - 1; i >= 0; i--) { reversed += input[i]; } return reversed; } } } |
In ReverseStringWithReverse
we take the incoming string and apply the built-in Reverse()
extension method on it. It returns a collection of chars
, i.e. not the reversed string. To join those chars into a string we can call string.Join
or string.Concat
. Join
takes a delimiter which is an empty string in this case as we don’t want to put any extra character in between the characters when they are joined. The Concat
function is more straightforward to use, we can just pass in the array collection.
In ReverseStringWithLoop
we iterate through the input string starting at the back. We build the reversed string character by character and return the result.
ReverseStringWithAggregate
is a bit more “exotic” and uses the Aggregate
LINQ operator. I only put it here in case you’d like to show your familiarity with this accumulator function in an interview. The empty string is a seed which is basically the starting point for building the reversed string.
This overload of the Aggregate
function will operate on an IEnumerable<char>
as the source string is dissected into its characters. We specified the seed as a string type so the overall result from this extension function will also be a string.
We start with an empty string and apply a function on the source string defined by
1 2 3 |
(c1, c2) => c2 + c1 |
This is the accumulator function and accepts two parameters: the first one, i.e. c1
will be of the same type as the seed, i.e. a string. The second one, i.e. c2
will be the same type as each element in the dissected string, i.e. a char. The accumulator function returns the same type as the seed, i.e. a string which is simply built up gradually as
1 2 3 |
c2 + c1 |
…i.e. take the character and add it to the growing string c1
.
Let’s try to understand this function using the input string “hello”.
So we start with an empty string as c1
and take the first character from the source char collection, i.e. ‘h’. We get "" + 'h' = "h"
as the resulting string which will be fed as the c1 string input into the next iteration. So next c1 will be “h” and c2 ‘e’ and we return ‘e’ + “h”, i.e. “eh”. “eh” is taken as c1 in the next round and ‘l’ as the character input, ‘l’ + “eh” will give “leh”. The function continues until we arrive at “olleh”. If we set the seed to “world” then that will be value of c1 in the very first iteration and the end result will be “ollehworld”.
It is probably overkill to use the Aggregate
extension for a problem like this but it’s a good opportunity to demonstrate your familiarity with this relatively rarely used function in an interview setting.