4 Technical Warmup Questions for Your Next Programming Interview
By: William Bennett
Having worked with programmers for years I know that often times recruiters are unable to or ignore the technical points that candidates have to deal with in their job search. Sure, there are many phases of an interview; the phone call, the face-to-face interview, etc. but, often times consultants overlook the technical. While both personal and technical skills are crucial, it is incredibly helpful to have a little coding warm-up before an interview. To balance this out I've compiled a list of 4 popular technical exercises to help you prepare for your next challenge.
Simple Patterns: Singleton Pattern
Design patterns offer proven ways to solve common architectural problems. There are many out there and it’s probably not reasonable to expect a candidate to know them all. However, depending on the years of experience a candidate has, they should be able to tell you one or two they have used in the past and more importantly why it’s useful to use a particular pattern.
Now, let’s start with the singleton pattern.
The singleton pattern enforces that one and only one instance of a class will ever exist within an application. This is relatively easy to do.
- Make all constructors of the class private.
- Create a private static member that’s of the same type as the class.
- Create a public static member that returns and an instance of that class.
When the static method is called, first check if the private member that’s of the same type of the class is initialized. If it isn’t, do so. Then return that member to the caller. Easy right?
This is simple and works well. If you are writing a multi-threaded app though, this won’t work. It's not threaded safe. Don’t worry though. It’s simple enough to fix. All you need to do is obtain a lock on a common object before initializing the instance.
Locking a common object blocks any other threads from accessing that code block until the current thread is done. Using this approach ensures that no other threads access the single instance while it’s being initialized.
Operator overloading and pairing rules in C#
What is the problem in the following code snippet?
When you overload equality (==, !=) and comparison (<, >, <=, >=) operators, special pairing and overriding rules are enforced by C# compiler. For the above example, the compiler generates 2 warnings and the 1 error:
- Warning: 'ConsoleApplication1.Point' defines operator == or operator != but does not override Object.GetHashCode()
- Warning: 'ConsoleApplication1.Point' defines operator == or operator != but does not override Object.Equals(object o)
- Error: The operator 'ConsoleApplication1.Point.operator ==(ConsoleApplication1.Point, ConsoleApplication1.Point)' requires a matching operator '!=' to also be defined
Rule 1: If you overload the equality operators (== and !=), it is good practice to override the GetHashCode() and Equals() methods. Reason: Collections and HashTables rely on equality relations to work reliably.
Rule 2: If you overload one operation that is part of a pair (for example, == and !=), you are required to implement the other operator.
Rule 3: If you overload the comparison operators (< and >), it is a good practice to implement IComparable and IComparable<T> for the same reasons defined for rule 1.
An HTTP module is a simple class that can plug themselves into the request-processing pipeline. They do this by hooking into a handful of events thrown by the application as it processes the HTTP request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.
To create an HttpModule, you simply create a class that derives from the System.Web.IHttpModule interface. This interface requires you to implement two methods: Init and Dispose. The simple code snippet below shows an HttpModule that appends a simple message at the end of every page output in your application.
To use this module, we need to modify the web.config and add a reference to this module. This will let ASP.NET know that you want to include the module in the request processing pipeline.
That’s it. HttpModules provide you with a simple and effective way to tap into the ASP.NET request pipeline.
LINQ - Group, Sort and Count Words in a sentence by length
LINQ has been around for some time now to make up to the programming interview list. With the proliferation of Entity Framework, this has become more popular. Let’s start our LINQ interview question series with a serious question:
Question: Given a sentence, group the words of the same length, sort the groups in increasing order and display the groups, the word count and the words.
For example, the following sentence: “LINQ can be used to group words and count them too” should result in an output like:
Words of length: 2, Count: 2
Words of length: 3, Count: 3
Words of length: 4, Count: 3
Words of length: 5, Count: 3
Let’s look at the code. The function to do solve this is quite simple. I have commented the code to highlight the LINQ operations that parse the sentence and converts it into a relevant solution.
I hope these questions have given you an opportunity to practice before your next interview. There are countless other questions and tasks I haven’t been able to tackle, but if you’d like to gain access to some more feel free to contact me via email firstname.lastname@example.org or on LinkedIn at https://www.linkedin.com/in/william-bennett-67a419102/.
Senior Microsoft .Net Consultant