At a recent interview I was asked to write an algorithm which:
Given a string text, and string subtext, finds the starting character positions of each subtext found within the text.
"hey hi how are you hi" = 5, 20
I was forbidden to use any System.String functions (Substring, IndexOf etc).
I wrote this in C#:
IEnumerable<int> CalculateSubtextPositions(string text, string subtext)
{
var charIndexToMatch = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] != subtext[charIndexToMatch])
charIndexToMatch = 0;
if (text[i] == subtext[charIndexToMatch])
{
charIndexToMatch++;
if (charIndexToMatch == subtext.Length)
{
yield return i - charIndexToMatch + 2;
charIndexToMatch = 0;
}
}
}
}
Which was described as 'disappointing'. It may well be I've missed something, but I can't see what's hugely wrong with the above. It works, it's efficient, and reasonably easy to read compared to many approaches (at least in my opinion, please correct me if I'm wrong).
Would people be able to suggest where I might have messed up? It may have been because I was asked to make the code 'reusable', but I'm not sure to what extent that can be applied when you're only asked to write an algorithm.
Please do criticise! :)
yieldand didn't want to admit it to you. You are likely better off not working there, therefore...charIndexToMatchwould count far more than working out edge cases in one's head. Each to their own, I guess...