JOBSEEKER?

C# Interview Questions

Practice for C# interviews by solving TestDome questions. Our interview questions are used by more than 7,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

Need to practice your C# programming skills for an upcoming job interview? Try solving these C# interview questions that test knowledge of C# programming concepts such as arrays, linked lists, serialization, inheritance, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.

These C# interview questions are examples of real tasks used by employers to screen job candidates such as C# developers, .NET developers, back-end developers, and others that require knowledge of the C# programming language and the .NET framework’s class library.

1. Cache Casting

Easy  
C# Inheritance OOP New Public

A company is designing the class hierarchy for various cache implementations:

public class Cache {}

public class DiskCache : Cache {}

public class MemoryCache : Cache {}

public class OptimizedDiskCache : DiskCache {}

Select all the answers that will result in a runtime exception.

(Select all acceptable answers.)


2. Merge Names

Easy  
C# Arrays Public

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.


3. Quadratic Equation

Easy  
C# Arithmetic New Public

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.


4. Binary Search Tree

Easy  
C# Algorithmic thinking Recursion Tree 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.


5. Song

Easy  
C# Algorithmic thinking HashSet Linked list Public

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());

6. User Input

Easy  
C# Inheritance 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());

7. Two Sum

Easy  
C# Algorithmic thinking Dictionary 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

8. Account

Hard  
C# Enum Public

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

9. Folders

Hard  
C# Serialization XML 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).


10. Sorted Search

Hard  
C# Algorithmic thinking Binary search 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.


11. Train Composition

Hard  
C# Algorithmic thinking Linked list 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.


12. Route Planner

Hard  
C# 2D array Algorithmic thinking Graphs New Public

As a part of the route planner, the RouteExists method is used as a quick filter if the destination is reachable, before using more computationally intensive procedures for finding the optimal route.

The roads on the map are rasterized and produce a matrix of boolean values - true if the road is present or false if it is not. The roads in the matrix are connected only if the road is immediately left, right, below or above it.

Finish the RouteExists method so that it returns true if the destination is reachable or false if it is not. The fromRow and fromColumn parameters are the starting row and column in the mapMatrix. The toRow and toColumn are the destination row and column in the mapMatrix. The mapMatrix parameter is the above mentioned matrix produced from the map.

For example, for the given rasterized map, the code below should return true since the destination is reachable:

bool[,] mapMatrix = {
	{true, false, false},
	{true, true, false},
	{false, true, true}
};

RouteExists(0, 0, 2, 2, mapMatrix);

13. Alert Service

Easy  
C# Interfaces Inversion of control Refactoring Public

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.

14. Account Test

Easy  
C# Test case design Unit testing Public

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.


15. Decorator Stream

Hard  
C# Stream Public

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.


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)

C#, MS SQL Server, 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.
Dashboard Start Trial Sign In Home Tour Tests Questions Pricing For Jobseekers