C# Interview Questions

Want to become an expert in cracking C# interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public C# Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.


1. Alert Service

C# Refactoring Public New

Refactor the AlertService and AlertDAO classes:

  • Create a new interface, named IAlertDAO, that contains the same methods as AlertDAO.
  • AlertDAO should implement the IAlertDAO interface.
  • AlertService should have a constructor that accepts IAlertDAO.
  • The RaiseAlert and GetAlertTime methods should use the object passed through the constructor.
Easy 
10min
C# 7.0, .NET Framework 4.7
 


  •   AlertDAO implements IAlertDAO interface: Wrong answer
  •   AlertService accepts IAlertDAO in its constructor: Wrong answer
  •   The RaiseAlert and GetAlertTime methods use IAlertDAO passed through constructor: Wrong answer


2. Merge Names

C# Arrays Public New

Implement the UniqueNames method. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.

For example, calling MergeNames.UniqueNames(new string[]{'Ava', 'Emma', 'Olivia'}, new string[]{'Olivia', 'Sophia', 'Emma'}) should return an array containing Ava, Emma, Olivia, and Sophia in any order.

Easy  
10min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at MergeNames.UniqueNames(String[] names1, String[] names2) in MergeNames.cs:line 7
  •   Each array has distinct names: System.NotImplementedException at MergeNames.UniqueNames(String[] names1, String[] names2) in MergeNames.cs:line 7
  •   Each array has duplicate names: System.NotImplementedException at MergeNames.UniqueNames(String[] names1, String[] names2) in MergeNames.cs:line 7
  •   Arrays have some names in common: System.NotImplementedException at MergeNames.UniqueNames(String[] names1, String[] names2) in MergeNames.cs:line 7


3. Palindrome

C# Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, IsPalindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.

Easy  
10min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at Palindrome.IsPalindrome(String word) in Palindrome.cs:line 7
  •   Lowercase words: System.NotImplementedException at Palindrome.IsPalindrome(String word) in Palindrome.cs:line 7
  •   Various words: System.NotImplementedException at Palindrome.IsPalindrome(String word) in Palindrome.cs:line 7


4. Account Test

C# Test case design Public New

Using only NUnit's Assert.AreEqual method, write tests for the Account class that cover the following cases:

  • The Deposit and Withdraw methods will not accept negative numbers.
  • Account cannot overstep its overdraft limit.
  • The Deposit and Withdraw methods will deposit or withdraw the correct amount, respectively.
  • The Withdraw and Deposit methods return the correct results.
public class Account
{
    public double Balance { get; private set; }
    public double OverdraftLimit { get; private set; }

    public Account(double overdraftLimit)
    {
        this.OverdraftLimit = overdraftLimit > 0 ? overdraftLimit : 0;
        this.Balance = 0;
    }

    public bool Deposit(double amount)
    {
        if (amount >= 0)
        {
            this.Balance += amount;
            return true;
        }
        return false;
    }

    public bool Withdraw(double amount)
    {
        if (this.Balance - amount >= -this.OverdraftLimit && amount >= 0)
        {
            this.Balance -= amount;
            return true;
        }
        return false;
    }
}

Each of the test case methods should be in the Tester class and have the Test attribute.

Easy 
15min
C# 7.0, .NET Framework 4.7
 


  •   The Deposit and Withdraw methods will not accept negative numbers: Wrong answer
  •   Account cannot overstep its overdraft limit: Wrong answer
  •   The Deposit and Withdraw methods will deposit or withdraw the correct amount, respectively: Wrong answer
  •   The Withdraw and Deposit methods return the correct results: Wrong answer


5. Binary Search Tree

C# Algorithmic thinking Data structures Public

A three-node binary tree.Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.

Write a function that, efficiently with respect to time used, checks if a given binary search tree contains a given value.

For example, for the following tree:

  • n1 (Value: 1, Left: null, Right: null)
  • n2 (Value: 2, Left: n1, Right: n3)
  • n3 (Value: 3, Left: null, Right: null)

Call to Contains(n2, 3) should return true since a tree with root at n2 contains number 3.

Easy  
15min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at BinarySearchTree.Contains(Node root, Int32 value) in BinarySearchTree.cs:line 23
  •   Correctness: System.NotImplementedException at BinarySearchTree.Contains(Node root, Int32 value) in BinarySearchTree.cs:line 23
  •   Performance test on a large tree: System.NotImplementedException at BinarySearchTree.Contains(Node root, Int32 value) in BinarySearchTree.cs:line 23


6. Playlist

C# Algorithmic thinking Searching Public New

A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to null.

Implement a function IsRepeatingPlaylist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not.

For example, the following code prints "True" as both songs point to each other.

Song first = new Song("Hello");
Song second = new Song("Eye of the tiger");
    
first.NextSong = second;
second.NextSong = first;

Console.WriteLine(first.IsRepeatingPlaylist());
Easy 
15min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Playlist.cs:line 15
  •   Song will repeat: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Playlist.cs:line 15
  •   Song will not repeat: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Playlist.cs:line 15
  •   Performance test on a large playlist: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Playlist.cs:line 15


7. User Input

C# OOP Public

User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput, which accepts only digits.

Implement the class TextInput that contains:

  • Public method void Add(char c) - adds the given character to the current value
  • Public method string GetValue() - returns the current value

Implement the class NumericInput that:

  • Inherits TextInput
  • Overrides the Add method so that each non-numeric character is ignored

For example, the following code should output "10":

TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input.GetValue());
Easy  
15min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.InvalidCastException
  •   Adding various characters to TextInput: System.NullReferenceException
  •   Adding numeric characters to NumericInput: System.InvalidCastException
  •   Adding various characters to NumericInput: System.InvalidCastException


8. Two Sum

C# Algorithmic thinking Searching Public

Write a function that, when passed a list and a target sum, returns, efficiently with respect to time used, two distinct zero-based indices of any two of the numbers, whose sum is equal to the target sum. If there are no two numbers, the function should return null.

For example, FindTwoSum(new List<int>() { 3, 1, 5, 7, 5, 9 }, 10) should return a Tuple<int, int> containing any of the following pairs of indices:

  • 0 and 3 (or 3 and 0) as 3 + 7 = 10
  • 1 and 5 (or 5 and 1) as 1 + 9 = 10
  • 2 and 4 (or 4 and 2) as 5 + 5 = 10
Easy  
30min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at TwoSum.FindTwoSum(IList`1 list, Int32 sum) in TwoSum.cs:line 8
  •   Distinct numbers with and without solutions: System.NotImplementedException at TwoSum.FindTwoSum(IList`1 list, Int32 sum) in TwoSum.cs:line 8
  •   Duplicate numbers with and without solutions: System.NotImplementedException at TwoSum.FindTwoSum(IList`1 list, Int32 sum) in TwoSum.cs:line 8
  •   Performance test with a large list of numbers: System.NotImplementedException at TwoSum.FindTwoSum(IList`1 list, Int32 sum) in TwoSum.cs:line 8


9. Account

C# Enum Public New

Each account on a website has a set of access flags that represent a users access.

Update and extend the enum so that it contains three new access flags:

  1. A Writer access flag that is made up of the Submit and Modify flags.
  2. An Editor access flag that is made up of the Delete, Publish and Comment flags.
  3. An Owner access that is made up of the Writer and Editor flags.

For example, the code below should print "False" as the Writer flag does not contain the Delete flag.

Console.WriteLine(Access.Writer.HasFlag(Access.Delete))
Hard  
7min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.ArgumentException
  •   Writer Access: System.ArgumentException
  •   Editor Access: System.ArgumentException
  •   Owner Access: System.ArgumentException


10. Decorator Stream

C# Stream Public New

Implement methods and properties in the DecoratorStream class:

  • Write method should write the prefix into the underlying stream member only on the first Write invocation. It should always write the bytes it receives to the underlying stream.
  • The prefix should be written in UTF-8 encoding.

For example, if the DecoratorStream is instantiated with "First line: " as the prefix parameter and Write method is called with UTF-8 byte representation of "Hello, world!", it should write "First line: Hello, world!" into the underlying stream.

Hard 
15min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at DecoratorStream.Write(Byte[] bytes, Int32 offset, Int32 count) in DecoratorStream.cs:line 29
  •   DecoratorStream writes to the stream: System.NotImplementedException at DecoratorStream.Write(Byte[] bytes, Int32 offset, Int32 count) in DecoratorStream.cs:line 29
  •   DecoratorStream writes the prefix using UTF-8 encoding: System.NotImplementedException at DecoratorStream.Write(Byte[] bytes, Int32 offset, Int32 count) in DecoratorStream.cs:line 29
  •   Multiple writes: System.NotImplementedException at DecoratorStream.Write(Byte[] bytes, Int32 offset, Int32 count) in DecoratorStream.cs:line 29


11. Folders

C# Serialization Public

Implement a function FolderNames, which accepts a string containing an XML file that specifies folder structure and returns all folder names that start with startingLetter. The XML format is given in the example below.

For example, for the letter 'u' and XML file:

<?xml version="1.0" encoding="UTF-8"?>
<folder name="c">
    <folder name="program files">
        <folder name="uninstall information" />
    </folder>
    <folder name="users" />
</folder>

the function should return "uninstall information" and "users" (in any order).

Hard  
20min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at Folders.FolderNames(String xml, Char startingLetter) in Folders.cs:line 8
  •   All folder names start with starting letter: System.NotImplementedException at Folders.FolderNames(String xml, Char startingLetter) in Folders.cs:line 8
  •   Root folder name starts with starting letter: System.NotImplementedException at Folders.FolderNames(String xml, Char startingLetter) in Folders.cs:line 8
  •   Complicated folder structure: System.NotImplementedException at Folders.FolderNames(String xml, Char startingLetter) in Folders.cs:line 8


12. Sorted Search

C# Algorithmic thinking Searching Public

Implement function CountNumbers that accepts a sorted array of unique integers and, efficiently with respect to time used, counts the number of array elements that are less than the parameter lessThan.

For example, SortedSearch.CountNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array elements less than 4.

Hard  
20min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at SortedSearch.CountNumbers(Int32[] sortedArray, Int32 lessThan) in SortedSearch.cs:line 7
  •   Various small arrays: System.NotImplementedException at SortedSearch.CountNumbers(Int32[] sortedArray, Int32 lessThan) in SortedSearch.cs:line 7
  •   Performance test when sortedArray contains lessThan: System.NotImplementedException at SortedSearch.CountNumbers(Int32[] sortedArray, Int32 lessThan) in SortedSearch.cs:line 7
  •   Performance test when sortedArray doesn't contain lessThan: System.NotImplementedException at SortedSearch.CountNumbers(Int32[] sortedArray, Int32 lessThan) in SortedSearch.cs:line 7


13. Train Composition

C# Algorithmic thinking Data structures Public

A TrainComposition is built by attaching and detaching wagons from the left and the right sides, efficiently with respect to time used.

For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.

Implement a TrainComposition that models this problem.

Two train carriages.

Hard  
20min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at TrainComposition.AttachWagonFromLeft(Int32 wagonId) in TrainComposition.cs:line 7
  •   Several wagons: System.NotImplementedException at TrainComposition.AttachWagonFromRight(Int32 wagonId) in TrainComposition.cs:line 12
  •   Performance test with a large number of wagons: System.NotImplementedException at TrainComposition.AttachWagonFromRight(Int32 wagonId) in TrainComposition.cs:line 12


14. Path

C# Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.

Notes:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as "..".
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example:

Path path = new Path("/a/b/c/d");
path.Cd("../x");
Console.WriteLine(path.CurrentPath);

should display '/a/b/c/x'.

Hard  
30min
C# 7.0, .NET Framework 4.7
 


  •   Example case: System.NotImplementedException at Path.Cd(String newPath) in Path.cs:line 14
  •   Selecting child directories: System.NotImplementedException at Path.Cd(String newPath) in Path.cs:line 14
  •   Selecting parent directories: System.NotImplementedException at Path.Cd(String newPath) in Path.cs:line 14
  •   Selecting complex paths: System.NotImplementedException at Path.Cd(String newPath) in Path.cs:line 14


If you feel ready, take one of our timed public C# Interview Questions tests:
  • ASP.NET (Core) MVC, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)
  • ASP.NET Web Forms, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)
  • C# Online Test (Easy / Hard)
  • C# Algorithms Online Test (Easy / Hard)
  • C# Algorithms and SQL Online Test (Easy / Hard)
  • C# and .NET Online Test (Easy / Hard)
  • C# and SQL Online Test (Easy / Hard)
  • HTML/CSS, JavaScript, C# Algorithms, and SQL Online Test (Easy / Hard)
  • HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)
Not exactly what you are looking for? Go to our For Jobseekers section.