A pragmatic coding excercise for C# developer’s job interview

Job interview questions are efficient when testing the theoretical knowledge of a developer however good answers do not necessarily indicate solid practical skills. If you cannot afford long interviews with multiple rounds then your possibilities are very limited. While complex development tasks needs too much time (and usually insight as well), small tasks are far too simple to involve enough conceptual issues.

The following exercise can solve these problems (within reason). Instead of asking for a proper design demo take a snippet of bad code and ask the developer to fix it without changing the original semantics. It is important to keep the original functionality to enforce fixing all the flaws and keep the task well focused. When creating the code snippet you can hide any OOP principle or language idiom inside the faults. But take care, too much of such code can be very confusing to clean out.

The example below demonstrates a test snippet. The basic OOP design principles inheritance, polymorphism, data hiding, encapsulation issues and some C# language elements are also hidden within flaws. Note, that the code compiles without any error or warning!

internal class Client
{
    public object Id;
    internal string RegistrationFilePath = "c:\\tmp\\tmp.txt";
    internal int status;
    public int STATUSREGISTERED = 1;
    public int STATUSNOTREGISTERED = 2;

    internal void RegisterClient(string z)
    {
        status = STATUSREGISTERED;
        var x = new System.IO.StreamWriter(RegistrationFilePath);
        x.WriteLine(Id + ": " + z);
    }
}

public class EnterpriseClient
{
    private Client _client = new Client();
    public void SetId(int id) { _client.Id = id; }
    public void RegisterClient(string s) { _client.RegisterClient(s); }
}

public class PrivateClient
{
    private Client _client = new Client();
    public List _remarks;
    public void SetId(Guid id) { _client.Id = id; }
    public void StoreRemarks(IEnumerable remarks)
    {
        if (remarks == null)
        {
            throw new Exception("Invalid remarks for private client.");
        }
        IEnumerator remarkEnumerator = remarks.GetEnumerator();
        _remarks = new List();
        do
        {
            _remarks.Add(remarkEnumerator.Current);
        } while (remarkEnumerator.MoveNext());
    }
    
    internal void RegisterClient(string s)
    {
        Console.WriteLine(_client.Id + ": " + s
            + ", remark count: " + _remarks.Count);
    }
}