Geek Culture
Published in

Geek Culture

C# Drills for Interview Preparation

A Tour of Must-Know C# Data Structures and Important Operations

Photo by Steve Johnson on Unsplash

Introduction

This article helps you if you’re already familiar with C# and you need to:

  1. Prepare for interviews, or
  2. Reacquint yourself with the language after a period away from C# e.g. moving from frontend development in Angular and TypeScript to a backend project.

Each part consists of a “drill” that explores important operations of a few common C# data structures.

Names of variables and methods are kept deliberately short to minimise the amount of typing required to complete the code exercises.

What this Article Is Not

This article is about “learning by doing” and doesn’t offer thorough explanations of the operations used. You’re left to explore these operations in more detail in the C# Documentation page in your own time.

Setup

Open a new Replit C# environment (or create a Visual Studio console project):

To help get you through the drill quicker, I’ve created a method “o” that take an object and prints out it’s string representation as follows:

Hit the Run button at the top and the output is written to the Console on the right-hand-side.

You’re now ready to begin the C# drills!

Strings

This part reuses the following strings:

string s1 = "ab";
string s2 = "ab";
string s3 = "b";

Drill

Note: whenever you see 🔥, it means that an exception is thrown.

o(string.Compare(s1,s2));           // 0
o(string.Compare(s1,s3)); // -1

o(s1.CompareTo(s2)); // 0
o(s1.CompareTo(s3)); // -1
o(string.Concat(s1,s3)); // "abb"o(s1.Contains(s3)); // True o(s1.StartsWith(s3)); // False
o(s1.EndsWith(s3)); // True
o(s1.Equals(s2)); // True
o(s1.Equals(s3)); // False
var e = s1.GetEnumerator();
while (e.MoveNext())
o(e.Current); // "a"
// "b"
o(s1.GetHashCode()); // -184055581o(s1.IndexOf("a")); // 0
o(s1.IndexOf("z")); // -1
o(s1.Insert(1, "-")); // "a-b"
o(s1.Insert(3, "-")); 🔥
o(string.IsNullOrEmpty(s1)); // False
o(string.IsNullOrEmpty("")); // True
o(string.IsNullOrEmpty(" ")); // False
o(string.IsNullOrWhiteSpace(" ")); // True
string[] s4 = { s2, s3 };
o(string.Join("-", s4)); // "ab-b"
o(s2.Remove(1)); // "a"
o(s2.Remove(0,1)); // "b"
o(s2.Remove(2,3)); 🔥
o(s1.Replace("a","c")); // "cb"
o(s1.Replace("z","c")); // "ab"
o((s1 + s2).Replace("a","c")); // "cbcb"
var s5 = "a-b";
foreach (var s in s5.Split('-'))
o(s); // "a"
// "b"
foreach (var s in s5.Split('+'))
o(s); // "a-b"
o(s1.Substring(0)); // "ab"
o(s1.Substring(1)); // "b"
o(s1.Substring(2)); // ""
o(s1.Substring(0,1)); // "a"
o(s1.Substring(2,3)); 🔥
foreach(var s in s1.ToCharArray())
o(s); // "a"
// "b"
var s6 = s1.ToUpper();
o(s6); // "AB"
o(s6.ToLower()); // "ab"
var s7 = " ab ";
o(s7.Trim()); // "ab"
o(s7.TrimStart()); // "ab "
o(s7.TrimEnd()); // " ab"

Array

This part reuses the following string array and 2D integer arrays respectively:

var h = new string[2] {"a","b"};var i = new int[3,2]{
{1,2},
{3,4},
{5,6}
};

Drill

h[0] = "c";
o(h[0]); // "c"

h[2] = "d"; 🔥
Array.Sort(h);for (var j = 0; j < h.Length; j++)
o(h[j]); // "b"
// "c"
Array.Reverse(h);foreach(var j in h)
o(j); // "c"
// "b"
i[0,0] = 10;
o(i[0,0]); // 10
o(i[0,1]); // 2
o(i[1,0]); // 3
o(i[1,1]); // 4
o(i[2,0]); // 5
o(i[2,1]); // 6
for (int j = 0; j < i.GetUpperBound(0); j++)
{
var s1 = i[j, 0];
var s2 = i[j, 1];
o($"{s1} {s2}"); // 10 2
// 3 4
}

StringBuilder

Import the following namespace:

using System.Text;

Create an overloaded console printing method to work with StringBuilder:

private static void o(StringBuilder sb)
{
Console.WriteLine(sb.ToString());
}

Drill

var sb1 = new StringBuilder("a");sb1.Append('b',2);
o(sb1); // "abb"
sb1.Insert(1,"c");
o(sb1); // "acbb"
sb1.Remove(1,1);
o(sb1); // "abb"
sb1.Replace("b", "d");
o(sb1); // "add"

Stack

Import the following namespace:

using System.Collections.Generic;

Drill

var s = new Stack<int>();
s.Push(10);
s.Push(20);
s.Push(30);
foreach (var i in s)
o(i); // 30
// 20
// 10
o(s.Contains(30)); // True
o(s.Pop()); // 30
o(s.Contains(30)); // False
o(s.Peek()); // 20
s.Clear();
o(s.Count); // 0

Queue

Again, this drill requires the following namespace:

using System.Collections.Generic;

Drill

var q = new Queue<int>();
q.Enqueue(10);
q.Enqueue(20);
q.Enqueue(30);
o(q.Contains(10)); // True
o(q.Contains(40)); // False
var a = new int[q.Count];
q.CopyTo(a, 0);
for (var i = 0; i < a.Length; i++)
o(a[i]); // 10
// 20
// 30
for (var i = a.Length - 1; i >= 0; i--)
o(a[i]); // 30
// 20
// 10
o(q.Dequeue()); // 10
o(q.Peek()); // 20
q.Clear(); //
o(q.Count); // 0

Dictionary

Again, this drill requires the following namespace:

using System.Collections.Generic;

Drill

var d = new Dictionary<int, string>();d.Add(1, "a");                             
d.Add(2, "b");
d.Add(3, "c");
o(d[1]); // "a"
o(d.ContainsKey(1)); // True
o(d.ContainsValue("d")); // False
d.Remove(1); foreach (var i in d.Keys)
o(i); // 2
// 3
foreach (var i in d.Values)
o(i); // "b"
// "c"
foreach (var i in d)
o($"{i.Key} {i.Value}"); // "2 b"
// "3 c"
d.Clear();
o(d.Count); // 0

List

Again, this drill requires the following namespace:

using System.Collections.Generic;

Drill

var l = new List<int>()
{
10, 20
};
var l2 = new List<int> { 30 };l.AddRange(l2);foreach (var i in l)
o(i); // 10
// 20
// 30
o(l[0]); // 10l.Insert(1, 100);
o(l[1]); // 100
l.Remove(100);
o(l[1]); // 20
o(l.Contains(10)); // True
o(l.IndexOf(30)); // 2
l.Clear();
o(l.Count); // 0

Thanks for reading! Let me know what you think in the comments section below, and don’t forget to subscribe 👍

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
George Marklow

George Marklow

George is a software engineer, author, blogger, and abstract artist who believes in helping others to make us happier and healthier.