I recently had an interview and got to phase 2 which is a coding assessment. One of the questions was:
Find the first non-repeating character in a string: ("DEFD" --> E)
I didn't pass the coding assessment and was wondering if the community can help me improve.
Solution:
/// <summary>
/// Assuming that letter case doesn't matter, I will convert letters to upper case.
/// I will return ' ' if the non repeating character does not exist.
///
/// </summary>
/// <param name="givenString">A string</param>
/// <returns>The first non repeating character or ' '</returns>
public static char FindFirstNonRepeatingCharacter(string givenString)
{
char nonRepeatingCharacter = ' ';
Dictionary<char, int> charCounter = new Dictionary<char, int>();
char[] charArray = givenString.ToUpper().ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
char currentChar = charArray[i];
if (charCounter.ContainsKey(currentChar)) {
charCounter[currentChar]++;
} else
{
charCounter.Add(currentChar, 1);
}
}
for (int i = 0; i < charArray.Length; i++)
{
char currentChar = charArray[i];
if (charCounter[currentChar] == 1)
{
nonRepeatingCharacter = currentChar;
break;
}
}
return nonRepeatingCharacter;
}
Test:
[TestMethod()]
public void FindFirstNonRepeatingTestCharacterAllUpperCase()
{
string testString = "DEFD";
char testChar = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testString);
Assert.IsFalse(testChar == 'D' && testChar == 'F');
Assert.AreEqual('E', testChar);
}
/// <summary>
/// Testing lower case entries.
/// </summary>
[TestMethod()]
public void FindFirstNonRepeatingCharacterTestAllLowerCase()
{
string testString = "dfed";
char testChar = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testString);
Assert.IsFalse(testChar == 'D' && testChar == 'E');
Assert.AreEqual('F', testChar);
}
/// <summary>
/// Testing mixed case entries.
/// </summary>
[TestMethod()]
public void FindFirstNonRepeatingCharacterTestMixedCasing()
{
string testString = "EeFd";
char testChar = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testString);
Assert.IsFalse(testChar == 'E' && testChar == 'D');
Assert.AreEqual('F', testChar);
}
/// <summary>
/// Testing numeric entries.
/// </summary>
[TestMethod()]
public void FindFirstNonRepeatingCharacterTestNumeric()
{
string testString = "11233455";
char testChar = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testString);
Assert.IsFalse(testChar == '1' && testChar == '3' && testChar == '4' && testChar == '5');
Assert.AreEqual('2', testChar);
}
/// <summary>
/// Testing entries with white space.
/// </summary>
[TestMethod()]
public void FindFirstNonRepeatingTestWhiteSpace()
{
string testStringOne = " ";
string testStringTwo = "";
string testStringThree = "D EF D";
char testCharOne = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testStringOne);
char testCharTwo = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testStringTwo);
char testCharThree = BlueWolfSolutions.FindFirstNonRepeatingCharacter(testStringThree);
Assert.IsFalse(testCharThree == 'D' && testCharThree == 'F');
Assert.IsTrue(testCharOne == ' ' && testCharTwo == ' ' && testCharThree == 'E');
}