C# Interview Questions

Practice for C# interviews by solving TestDome questions. Our interview questions are used by more than 5,000 companies and 450,000 individual test takers.

Jobseekers: Certify Your Knowledge

Take a Certification Test

Companies: Use Our Tests for Screening

Buy a Pack Of Candidates

Try to solve 13 C# interview questions below. Hints can help you find answers to questions you are having trouble with.

1. Merge Names

Arrays
   
Easy  

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.

C# 8.0, .NET Framework 4.8  
 


  •   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


2. Quadratic Equation

Arithmetic
   
Easy  

Implement the FindRoots function to find the roots of the quadratic equation: ax2 + bx + c = 0. If the equation has only one solution, the function should return that solution as both elements of the tuple. The equation will always have at least one solution.

The roots of the quadratic equation can be found with the following formula: A quadratic equation.

For example, the roots of the equation 2x2 + 10x + 8 = 0 are -1 and -4.

C# 8.0, .NET Framework 4.8  
 


  •   Example case: System.NotImplementedException at QuadraticEquation.FindRoots(Double a, Double b, Double c) in QuadraticEquation.cs:line 7
  •   Equal roots: System.NotImplementedException at QuadraticEquation.FindRoots(Double a, Double b, Double c) in QuadraticEquation.cs:line 7
  •   Distinct roots: System.NotImplementedException at QuadraticEquation.FindRoots(Double a, Double b, Double c) in QuadraticEquation.cs:line 7


3. Binary Search Tree

Algorithmic thinking Recursion Tree
   
Easy  

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.

C# 8.0, .NET Framework 4.8  
 


  •   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


4. Song

Algorithmic thinking HashSet Linked list
   
Easy  

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());
C# 8.0, .NET Framework 4.8  
 


  •   Example case: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Song.cs:line 15
  •   If playlist repeats, last song is followed by first song: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Song.cs:line 15
  •   If playlist repeats, starting songs can be skipped: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Song.cs:line 15
  •   Performance test on a large playlist: System.InvalidOperationException at Song.IsRepeatingPlaylist() in Song.cs:line 15


5. User Input

Inheritance OOP
   
Easy  

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());
C# 8.0, .NET Framework 4.8  
 


  •   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


6. Two Sum

Algorithmic thinking Dictionary
   
Easy  

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
C# 8.0, .NET Framework 4.8  
 


  •   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


7. Account

Enum
   
Hard  

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))
C# 8.0, .NET Framework 4.8  
 


  •   Example case: Wrong answer
  •   Writer Access: Wrong answer
  •   Editor Access: Wrong answer
  •   Owner Access: Wrong answer


8. Folders

Serialization XML
   
Hard  

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).

C# 8.0, .NET Framework 4.8  
 


  •   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


9. Sorted Search

Algorithmic thinking Binary search
   
Hard  

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.

C# 8.0, .NET Framework 4.8  
 


  •   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


10. Train Composition

Algorithmic thinking Linked list
   
Hard  

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.

C# 8.0, .NET Framework 4.8  
 


  •   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


11. Alert Service

Interfaces Inversion of control Refactoring
   
Easy  

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.
C# 8.0, .NET Framework 4.8  
 


  •   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


12. Account Test

Test case design Unit testing
   
Easy  

Using only NUnitLite 1'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.

C# 8.0, .NET Framework 4.8  
 


  •   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


13. Decorator Stream

Stream
   
Hard  

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.

C# 8.0, .NET Framework 4.8  
 


  •   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


If you feel ready, take one of our timed public C# Interview Questions tests:
C#

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

HTML/CSS, JavaScript, C# Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

ASP.NET (Core) MVC

ASP.NET (Core) MVC, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

ASP.NET Web Forms

ASP.NET Web Forms, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.