I have been doing this test task for C# Developer role at UBS.London recently and have been rejected. No reason stated yet. Was wondering if you can take a look and provide feedback on what I can do to improve solution. The task description is below and solution itself is on GitHub. Also attached code for two main classes I have implemented. Unit tests are in solution. No UI provided as I thought it is not required as Unit tests are self explanatory.
Hope it will be helpful for someone else doing interviews for C# Dev roles.
Create a .net application that will solve the following problem. There are no time constraints and you are free to use any resources at your disposal.
The Problem As an author I want to know the number of times each word appears in a sentence So that I can make sure I'm not repeating myself
Acceptance Criteria Given a sentence When the program is run Then I'm returned a distinct list of words in the sentence and the number of times they have occurred
Example Input: "This is a statement, and so is this." Output: this - 2 is – 2 a – 1 statement – 1 and – 1 so - 1
I am also using Netfx-Guard library from Nuget
Solutions contains two projects: main project with SentenceParser
and StringSplitter
classes and unit testing projects.
SentenceParser
and StringSplitter
classes are below.
using System.Collections.Generic;
using System.Linq;
using UBS.TextParsing.Interfaces;
namespace UBS.TextParsing
{
public class SentenceParser
{
private IStringSplitter _splitter;
public SentenceParser(IStringSplitter splitter)
{
_splitter = splitter;
}
public IDictionary<string, int> Parse(string sentence)
{
Guard.NotNullOrEmpty(() => sentence, sentence);
var words = _splitter.SplitIntoWords(sentence);
var wordsCounts = from word in words
group word by word.ToLower()
into g
select new {Word = g.Key, Count = g.Count()};
return wordsCounts.ToDictionary(wc => wc.Word, wc => wc.Count);
}
}
}
Here is StringSplitter
class which splits sentence into words.
using System;
using System.Collections.Generic;
using System.Text;
using UBS.TextParsing.Interfaces;
namespace UBS.TextParsing
{
public class StringSplitter : IStringSplitter
{
public IEnumerable<string> SplitIntoWords(string sentence)
{
Guard.NotNull(() => sentence, sentence);
var stringList = new List<string>();
var currentWordSb = new StringBuilder();
foreach (var chr in sentence)
{
if (Char.IsPunctuation(chr) || Char.IsSeparator(chr) || Char.IsWhiteSpace(chr))
{
AddToList(currentWordSb, stringList);
currentWordSb.Clear();
}
else
{
currentWordSb.Append(chr);
}
}
AddToList(currentWordSb, stringList);
return stringList;
}
private void AddToList(StringBuilder stringBuilder, ICollection<string> collection)
{
var word = stringBuilder.ToString();
if(!string.IsNullOrEmpty(word))
collection.Add(word);
}
}
}