02 C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
02 C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
Introduction
In this article, we will learn about some of the frequently asked C# programming questions in
technical interviews.
Note: We won’t be using any inbuilt functions such as Reverse, Substring etc. for string manipulation,
also we will avoid using LINQ as these are generally restricted to be used in coding interviews.
Source Code
Download the source code for all the questions from GitHub (https://github.com/AnkitSharma-
007/csharp-interview-questions)
if we pass an integer as a string parameter then also this method will give the correct output
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 2/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
27. reverseSentence.Append(str[i]);
28. }
29. Console.WriteLine(reverseSentence.ToString());
30. }
output:
h–1
e–1
l–3
o–2
w – 1& Cookies: This site uses cookies. By continuing to use this website, you agree to their
Privacy
use.
Torfind
– 1 out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
d–1
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 3/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
1. internal static void Countcharacter(string str)
2. {
3. Dictionary<char, int> characterCount = new Dictionary<char, int>();
4.
5. foreach (var character in str)
6. {
7. if (character != ' ')
8. {
9. if (!characterCount.ContainsKey(character))
10. {
11. characterCount.Add(character, 1);
12. }
13. else
14. {
15. characterCount[character]++;
16. }
17. }
18.
19. }
20. foreach (var character in characterCount)
21. {
22. Console.WriteLine("{0} - {1}", character.Key, character.Value);
23. }
24. }
input: 1 2 3 4 5, output: 2 3 4 5 1
1. internal static void RotateLeft(int[] array)
2. {
3. int size = array.Length;
4. int temp;
5. for (int j = size - 1; j > 0; j--)
6. {
7. temp = array[size - 1];
8. array[array.Length - 1] = array[j - 1];
9. array[j - 1] = temp;
10. }
11.
12. foreach (int num in array)
13. {
14. Console.Write(num + " ");
15. }
16. }
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 5/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
The logic is to find a positive integer less than or equal to the square root of input integer. If there is
a divisor of number that is less than the square root of number, then there will be a divisor of
number that is greater than square root of number. Hence, we have to traverse till the square root
of number.
The time complexity of this function is O(√N) because we traverse from 1 to √N.
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 6/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
6. sum += num % 10;
7. num /= 10;
8. }
9. Console.WriteLine(sum);
10. }
Q.12: How to find second largest integer in an array using only one
loop?
Ans.: The user will input an unsorted integer array and the method should find the second largest
integer in the array.
input: 3 2 1 5 4, output: 4
1. internal static void FindSecondLargeInArray(int[] arr)
2. {
3. int max1 = int.MinValue;
4. int max2 = int.MinValue;
5.
6. foreach (int i in arr)
7. {
8. if (i > max1)
9. {
10. max2 = max1;
11. max1 = i;
12. }
13. else if (i >= max2 && i != max1)
14. {
15. max2 = i;
16. }
17. }
18. Console.WriteLine(max2); ;
19. }
Q.13: How to find third largest integer in an array using only one
loop?
Ans.: The user will input an unsorted integer array and the method should find the third largest
integer in the array.
input: 3 2 1 5 4, output: 3
1. internal static void FindthirdLargeInArray(int[] arr)
2. {
3. int max1 = int.MinValue;
4. int max2 = int.MinValue;
5. int max3 = int.MinValue;
6.
7. foreach (int i in arr)
8. {
9. if (i > max1)
10. {
11. max3
Privacy & Cookies: This site uses = max2;
cookies. By continuing to use this website, you agree to their
use. 12. max2 = max1;
13. max1 = i;
To find out more, including how to control cookies, see here: Cookie Policy
14. }
(https://ankitsharmablogs.com/privacy-policy/)
15. else if (i > max2 && i != max1)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 7/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
16. {
17. max3 = max2;
18. max2 = i;
19. }
20. else if (i > max3 && i != max2 && i != max1)
21. {
22. max3 = i;
23. }
24. }
25. Console.WriteLine(max3); ;
26. }
input: { { 1, 2, 3 }, { 4, 5, 6 } }, output: 1 4 2 5 3 6
1. internal static void MultiToSingle(int[,] array)
2. {
3. int index = 0;
4. int width = array.GetLength(0);
5. int height = array.GetLength(1);
6. int[] single = new int[width * height];
7.
8. for (int y = 0; y < height; y++)
9. {
10. for (int x = 0; x < width; x++)
11. {
12. single[index] = array[x, y];
13. Console.Write(single[index] + " ");
14. index++;
15. }
16. }
17. }
This question can also be asked to form a 1-D array row-wise. In this case, just swap the sequence
of the for loops as shown below. The output will be 1 2 3 4 5 6 for the input matrix mentioned above.
1. for (int x = 0; x < width; x++ )
2. {
3. for ( int y = 0; y < height; y++)
4. {
5. single[index] = array[x, y];
6. Console.Write(single[index] + " ");
7. index++;
8. }
9. }
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 8/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
123
456
1. internal static void SingleToMulti(int[] array, int row, int column)
2. {
3. int index = 0;
4. int[,] multi = new int[row, column];
5.
6. for (int y = 0; y < row; y++)
7. {
8. for (int x = 0; x < column; x++)
9. {
10. multi[y, x] = array[index];
11. index++;
12. Console.Write(multi[y, x] + " ");
13. }
14. Console.WriteLine();
15. }
16. }
Q.16: How to find the angle between hour and minute hands of a
clock at any given time?
Ans.: The user will input the hour and minute of the time and the method should give the angle
between the hour hand and minute hand at that given time.
input: 9 30, output: The angle between hour hand and minute hand is 105 degrees
input: 13 30, output: The angle between hour hand and minute hand is 135 degrees
The logic is to find the difference in the angle of an hour and minute hand from the position of 12 O
Clock when the angle between them is zero. Each hour on the clock represents an angle of 30
degrees (360 divided by 12). Similarly, each minute on the clock will represent an angle of 6 degrees
(360 divided by 60) and the angle for an hour will increase as the minutes for that hour increases.
The worst-case time complexity of this algorithm is O(N²). The best-case and average-case time
complexity of this algorithm is O(NLogN).
Divide the unsorted array of size N into N subarrays having single element each.
Take two adjacent subarrays and merge them to form a sorted subarray having 2
elements.Now we have N/2 subarrays of size 2.
Repeat the process until a single sorted array is obtained.
In all three cases (worst, average, best), the time complexity of Merge sort is O(NLogN)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 10/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
To place the element in its correct position in a sorted array, all the elements larger than the current
element are shifted one place ahead. Thus the sorted array will grow at each iteration.
Every element is compared to every other element of a sorted array. Hence the complexity of
Insertion sort is O(n²).
Read Insertion Sort Algorithm In C# (https://ankitsharmablogs.com/insertion-sort-algorithm-in-c-
sharp/) to learn more.
This algorithm searches a sorted array by repeatedly dividing the search interval in half. Begin with
an interval covering the whole array. If the value of the search key is less than the item in the middle
of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty. If found return the index of
matched element, else return -1.
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 11/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
Summary
I hope you like this collection of interview questions. If you have any suggestions or want me to
include any new questions, please let me know through the contact
(https://ankitsharmablogs.com/contact/) page.
You can download the source code for this article from GitHub (https://github.com/AnkitSharma-
007/csharp-interview-questions) and play around to get a better understanding.
You can also read my other articles here (https://ankitsharmablogs.com/)
See Also
JavaScript Interview Questions (https://learnersbucket.com/examples/topics/interview/)
Search...
FOLLOW ME
(https:/ (https:/
/www.f (https:/ /www.l (https:/ (https:/
aceboo /twitte inkedi /mediu /github
k.com/ r.com/ n.com/ m.com .com/A
Ankit.S ankitsh in/anki /@anki nkitSh
harma. arma_ tsharm tsharm arma-
0709) 007) a-007/) ablog) 007)
BUY ME A COFFEE
Buy me a coffee(https://www.buymeacoffee.com/ankitsharma)
FOLLOW MY BLOG
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.SUBSCRIBE TO BLOG VIA EMAIL
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
Enter your email address to subscribe to this blog and receive notifications of new posts by email.
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 12/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
Email Address
SUBSCRIBE
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.
To find out more, including how to control cookies, see here: Cookie Policy
(https://www.amazon.in/gp/product/178934414X/ref=as_li_tl?
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 13/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
ie=UTF8&camp=3638&creative=24630&creativeASIN=178934414X&linkCode=as2&tag=007056f-
21&linkId=594bb7b7027f32d6fe66badf87acd038)
GET MY BOOK ON C#
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.(https://www.amazon.in/gp/product/B0862DM82C/ref=as_li_tl?
To find out more, including how to control cookies, see here: Cookie Policy
ie=UTF8&camp=3638&creative=24630&creativeASIN=B0862DM82C&linkCode=as2&tag=007056f-
(https://ankitsharmablogs.com/privacy-policy/)
21&linkId=add223fa9297e51c756d4390d92b6dd4)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 14/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
FREE EBOOK ON ANGULAR AND FIREBASE
(https://www.c-sharpcorner.com/ebooks/build-a-full-stack-web-
application-using-angular-and-firebase)
CATEGORIES
Blazor (https://ankitsharmablogs.com/category/blazor/) 20
C# (https://ankitsharmablogs.com/category/c/) 17
Angular (https://ankitsharmablogs.com/category/angular/) 15
Algorithm (https://ankitsharmablogs.com/category/algorithm/) 6
Azure (https://ankitsharmablogs.com/category/azure/) 6
Angular 5 (https://ankitsharmablogs.com/category/angular-5/) 4
RECENT POSTS
Privacy
Going & Cookies: This With
Serverless site uses cookies.
Blazor By continuing to use this website, you agree to their
(https://ankitsharmablogs.com/going-serverless-with-blazor/) June 12,
use.2020
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 15/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
POPULAR POSTS
ARCHIVES
Select Month
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 16/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
GDE FOR ANGULAR
(https://developers.google.com/community/experts/directory/profile/profile-ankit-sharma)
MICROSOFT MVP
(https://mvp.microsoft.com/en-us/PublicProfile/5004063?fullName=Ankit%20Sharma)
C# CORNER MVP
(https://www.c-sharpcorner.com/members/ankit-
sharma93)
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 17/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
TAGS
Angular 5 (https://ankitsharmablogs.com/tag/angular-5/)
Angular 6 (https://ankitsharmablogs.com/tag/angular-6/)
Angular 7 (https://ankitsharmablogs.com/tag/angular-7/)
Authentication (https://ankitsharmablogs.com/tag/authentication/)
Authorization (https://ankitsharmablogs.com/tag/authorization/)
Azure (https://ankitsharmablogs.com/tag/azure/)
C# (https://ankitsharmablogs.com/tag/c/)
CRUD (https://ankitsharmablogs.com/tag/crud/)
Firebase (https://ankitsharmablogs.com/tag/firebase/)
Highcharts (https://ankitsharmablogs.com/tag/highcharts/)
Quantum
Privacy Computer
& Cookies: (https://ankitsharmablogs.com/tag/quantum-computer/)
This site uses cookies. By continuing to use this website, you agree to their
use.
Quantum Development Kit (https://ankitsharmablogs.com/tag/quantum-development-kit/)
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
server-side Blazor (https://ankitsharmablogs.com/tag/server-side-blazor/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 18/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
Serverless (https://ankitsharmablogs.com/tag/serverless/)
Typescript (https://ankitsharmablogs.com/tag/typescript/)
BLOG STATS
1,182,135 visits
POPULAR POSTS
BlazorGrid
Privacy & Cookies: – Auses
This site Reusable
cookies.Grid Component
By continuing to useFor
thisBlazor (https://ankitsharmablogs.com/blazorgrid-
website, you agree to their
use. reusable-grid-component-for-blazor/)
To find out more, including how to control cookies, see here: Cookie Policy
December 2, 2018
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 19/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
CATEGORIES
Blazor (https://ankitsharmablogs.com/category/blazor/) 20
C# (https://ankitsharmablogs.com/category/c/) 17
Angular (https://ankitsharmablogs.com/category/angular/) 15
Algorithm (https://ankitsharmablogs.com/category/algorithm/) 6
Azure (https://ankitsharmablogs.com/category/azure/) 6
Angular 5 (https://ankitsharmablogs.com/category/angular-5/) 4
FOLLOW ME
(https:/ (https:/
/www.f (https:/ /www.l (https:/ (https:/
aceboo /twitte inkedi /mediu /github
k.com/ r.com/ n.com/ m.com .com/A
Ankit.S ankitsh in/anki /@anki nkitSh
harma. arma_ tsharm tsharm arma-
0709) 007) a-007/) ablog) 007)
BLOG STATS
1,182,135 hits
HOME (HTTPS://ANKITSHARMABLOGS.COM)
Privacy
ABOUT & Cookies: This site uses cookies. By continuing to useCONTACT
(HTTPS://ANKITSHARMABLOGS.COM/ABOUT/) this website, you agree to their
(HTTPS://ANKITSHARMABLOGS.COM/CONTACT/)
use.
To find out more, including how to control cookies, see here: Cookie Policy
PRIVACY POLICY (HTTPS://ANKITSHARMABLOGS.COM/PRIVACY-POLICY/)
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 20/21
12/2/24, 2:11 PM C# Coding Questions For Technical Interviews - Ankit Sharma's Blog
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their
use.
To find out more, including how to control cookies, see here: Cookie Policy
(https://ankitsharmablogs.com/privacy-policy/)
https://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 21/21