├── LICENSE ├── Program.cs ├── Properties └── AssemblyInfo.cs ├── Questions.csproj ├── README.md ├── bin └── Debug │ └── Questions.vshost.exe.manifest └── obj └── Debug └── Questions.csproj.FileListAbsolute.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ankit 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Questions 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; 13 | MultiToSingle(arr); 14 | Console.ReadLine(); 15 | } 16 | 17 | // Q.1: How to reverse a string? 18 | internal static void ReverseString(string str) 19 | { 20 | /* input:- hello ; output:- olleh 21 | * input:- hello world ; output:- dlrow olleh 22 | * 23 | * */ 24 | char[] charArray = str.ToCharArray(); 25 | for (int i = 0, j = str.Length - 1; i < j; i++, j--) 26 | { 27 | charArray[i] = str[j]; 28 | charArray[j] = str[i]; 29 | } 30 | string reversedstring = new string(charArray); 31 | Console.WriteLine(reversedstring); 32 | } 33 | 34 | // Q.2: How to find if the given string is a palindrome or not? 35 | internal static void chkPalindrome(string str) 36 | { 37 | /* input:- madam ; output:- Palindrome 38 | * input:- step on no pets ; output:- Palindrome 39 | * input:- book ; output:- Not Palindrome 40 | * 41 | * */ 42 | 43 | bool flag = false; 44 | for (int i = 0, j = str.Length - 1; i < str.Length / 2; i++, j--) 45 | { 46 | if (str[i] != str[j]) 47 | { 48 | flag = false; 49 | break; 50 | } 51 | else 52 | flag = true; 53 | } 54 | if (flag) 55 | { 56 | Console.WriteLine("Palindrome"); 57 | } 58 | else 59 | Console.WriteLine("Not Palindrome"); 60 | } 61 | 62 | // Q.3: How to reverse the order of words in a given string? 63 | internal static void ReverseWordOrder(string str) 64 | { 65 | /* input:- Welcome to Csharp corner ; output:- corner Csharp to Welcome 66 | * 67 | * */ 68 | 69 | int i; 70 | StringBuilder reverseSentence = new StringBuilder(); 71 | 72 | int Start = str.Length - 1; 73 | int End = str.Length - 1; 74 | 75 | while (Start > 0) 76 | { 77 | if (str[Start] == ' ') 78 | { 79 | i = Start + 1; 80 | while (i <= End) 81 | { 82 | reverseSentence.Append(str[i]); 83 | i++; 84 | } 85 | reverseSentence.Append(' '); 86 | End = Start - 1; 87 | } 88 | Start--; 89 | } 90 | 91 | for (i = 0; i <= End; i++) 92 | { 93 | reverseSentence.Append(str[i]); 94 | } 95 | Console.WriteLine(reverseSentence.ToString()); 96 | } 97 | 98 | // Q.4: How to reverse each word in a given string? 99 | internal static void ReverseWords(string str) 100 | { 101 | /* input:- Welcome to Csharp corner ; output:- emocleW ot prahsC renroc 102 | * 103 | * */ 104 | 105 | StringBuilder output = new StringBuilder(); 106 | List charlist = new List(); 107 | 108 | for (int i = 0; i < str.Length; i++) 109 | { 110 | if (str[i] == ' ' || i == str.Length - 1) 111 | { 112 | if (i == str.Length - 1) 113 | charlist.Add(str[i]); 114 | for (int j = charlist.Count - 1; j >= 0; j--) 115 | output.Append(charlist[j]); 116 | 117 | output.Append(' '); 118 | charlist = new List(); 119 | } 120 | else 121 | charlist.Add(str[i]); 122 | } 123 | Console.WriteLine(output.ToString()); 124 | } 125 | 126 | // Q.5: How to count the occurrence of each character in a string? 127 | internal static void Countcharacter(string str) 128 | { 129 | /* input:- hello world ; 130 | * output:- h - 1 131 | e - 1 132 | l - 3 133 | o - 2 134 | w - 1 135 | r - 1 136 | d - 1 137 | * 138 | * */ 139 | 140 | Dictionary characterCount = new Dictionary(); 141 | 142 | foreach (var character in str) 143 | { 144 | if (character != ' ') 145 | { 146 | if (!characterCount.ContainsKey(character)) 147 | { 148 | characterCount.Add(character, 1); 149 | } 150 | else 151 | { 152 | characterCount[character]++; 153 | } 154 | } 155 | 156 | } 157 | foreach (var character in characterCount) 158 | { 159 | Console.WriteLine("{0} - {1}", character.Key, character.Value); 160 | } 161 | } 162 | 163 | // Q.6: How to remove duplicate characters from a string? 164 | internal static void removeduplicate(string str) 165 | { 166 | /* input:- csharpcorner ; output:- csharpone 167 | * 168 | */ 169 | string result = string.Empty; 170 | 171 | for (int i = 0; i < str.Length; i++) 172 | { 173 | if (!result.Contains(str[i])) 174 | { 175 | result += str[i]; 176 | } 177 | } 178 | Console.WriteLine(result); 179 | } 180 | 181 | // Q.7: How to find all possible substring of a given string? 182 | internal static void findallsubstring(string str) 183 | { 184 | for (int i = 0; i < str.Length; ++i) 185 | { 186 | StringBuilder subString = new StringBuilder(str.Length - i); 187 | for (int j = i; j < str.Length; ++j) 188 | { 189 | subString.Append(str[j]); 190 | Console.Write(subString + " "); 191 | } 192 | } 193 | } 194 | 195 | // Q.8: How to perform Left circular rotation of an array? 196 | internal static void RotateLeft(int[] array) 197 | { 198 | /* input :- 1 2 3 4 5 output :- 2 3 4 5 1 199 | * */ 200 | 201 | //Logic :- Iterate loop from size-1 to 0 and swap each elements with last element 202 | 203 | int size = array.Length; 204 | int temp; 205 | for (int j = size - 1; j > 0; j--) 206 | { 207 | temp = array[size - 1]; 208 | array[array.Length - 1] = array[j - 1]; 209 | array[j - 1] = temp; 210 | } 211 | 212 | foreach (int num in array) 213 | { 214 | Console.Write(num + " "); 215 | } 216 | } 217 | 218 | // Q.9: How to perform Right circular rotation of an array? 219 | internal static void RotateRight(int[] array) 220 | { 221 | /* input :- 1 2 3 4 5 output :- 5 1 2 3 4 222 | * */ 223 | 224 | //Logic 1 :- Iterate loop from 0 to size-1 and swap each elements with first element 225 | 226 | int size = array.Length; 227 | int temp; 228 | for (int j = 0; j < size - 1; j++) 229 | { 230 | temp = array[0]; 231 | array[0] = array[j + 1]; 232 | array[j + 1] = temp; 233 | } 234 | foreach (int num in array) 235 | { 236 | Console.Write(num + " "); 237 | } 238 | } 239 | 240 | // Q.10: How to find if a positive integer is a prime number or not? 241 | internal static bool FindPrime(int number) 242 | { 243 | /* input :- 20 output :- Not Prime 244 | * input :- 17 output :- Prime 245 | * 246 | * */ 247 | if (number == 1) return false; 248 | if (number == 2) return true; 249 | if (number % 2 == 0) return false; 250 | 251 | var squareRoot = (int)Math.Floor(Math.Sqrt(number)); 252 | 253 | for (int i = 3; i <= squareRoot; i += 2) 254 | { 255 | if (number % i == 0) return false; 256 | } 257 | 258 | return true; 259 | } 260 | 261 | // Q.11: How to find the sum of digits of a positive integer? 262 | internal static void SumOfDigits(int num) 263 | { 264 | /* input:- 168 ; output:- 15 265 | * 266 | * */ 267 | 268 | int sum = 0; 269 | while (num > 0) 270 | { 271 | sum += num % 10; 272 | num /= 10; 273 | } 274 | Console.WriteLine(sum); 275 | } 276 | 277 | // Q.12: How to find second largest integer in an array using only one loop? 278 | internal static void FindSecondLargeInArray(int[] arr) 279 | { 280 | /* input :- 1 2 3 4 5 output :- 4 281 | * */ 282 | 283 | int max1 = int.MinValue; 284 | int max2 = int.MinValue; 285 | 286 | foreach (int i in arr) 287 | { 288 | if (i > max1) 289 | { 290 | max2 = max1; 291 | max1 = i; 292 | } 293 | else if (i > max2) 294 | { 295 | max2 = i; 296 | } 297 | } 298 | Console.WriteLine(max2); ; 299 | } 300 | 301 | // Q.13: How to find Third largest integer in an array using only one loop? 302 | internal static void FindthirdLargeInArray(int[] arr) 303 | { 304 | int max1 = int.MinValue; 305 | int max2 = int.MinValue; 306 | int max3 = int.MinValue; 307 | 308 | foreach (int i in arr) 309 | { 310 | if (i > max1) 311 | { 312 | max3 = max2; 313 | max2 = max1; 314 | max1 = i; 315 | } 316 | else if (i > max2) 317 | { 318 | max3 = max2; 319 | max2 = i; 320 | } 321 | else if (i > max3) 322 | { 323 | max3 = i; 324 | } 325 | } 326 | Console.WriteLine(max3); ; 327 | } 328 | 329 | // Q.14: How to convert a two-dimensional array to a one-dimensional array? 330 | internal static void MultiToSingle(int[,] array) 331 | { 332 | //Convert 2-D array to 1-D array 333 | 334 | /*Input: { { 1, 2, 3 }, { 4, 5, 6 } }, output: 1 4 2 5 3 6 335 | * 336 | * */ 337 | 338 | int index = 0; 339 | int width = array.GetLength(0); 340 | int height = array.GetLength(1); 341 | int[] single = new int[width * height]; 342 | 343 | for (int y = 0; y < height; y++) 344 | { 345 | for (int x = 0; x < width; x++) 346 | { 347 | single[index] = array[x, y]; 348 | Console.Write(single[index] + " "); 349 | index++; 350 | } 351 | } 352 | } 353 | 354 | // Q.15: How to convert a one-dimensional array to a two-dimensional array? 355 | internal static void SingleToMulti(int[] array, int row, int column) 356 | { 357 | //convert 1-D array to 2-D array 358 | 359 | /* input: 1, 2, 3, 4, 5, 6 360 | * output: 1 2 3 361 | * 4 5 6 362 | * 363 | * */ 364 | 365 | int index = 0; 366 | int[,] multi = new int[row, column]; 367 | 368 | for (int y = 0; y < row; y++) 369 | { 370 | for (int x = 0; x < column; x++) 371 | { 372 | multi[y, x] = array[index]; 373 | index++; 374 | Console.Write(multi[y, x] + " "); 375 | } 376 | Console.WriteLine(); 377 | } 378 | } 379 | 380 | // Q.16: How to find the angle between hour and minute hands of a clock at any given time? 381 | 382 | internal static void FindAngleinTime(int hours, int mins) 383 | { 384 | /* input :- 9 30 output :- The angle between hour hand and minute hand is 105 degrees 385 | * input :- 13 30 output :- The angle between hour hand and minute hand is 135 degrees 386 | * 387 | */ 388 | 389 | double hourDegrees = (hours * 30) + (mins * 30.0 / 60); 390 | double minuteDegrees = mins * 6; 391 | 392 | double diff = Math.Abs(hourDegrees - minuteDegrees); 393 | 394 | if (diff > 180) 395 | { 396 | diff = 360 - diff; 397 | } 398 | 399 | Console.WriteLine("The angle between hour hand and minute hand is {0} degrees", diff); 400 | } 401 | } 402 | } 403 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Questions")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Questions")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5cc16e2e-bfda-4c43-8c08-9042385561c2")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Questions.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {5CC16E2E-BFDA-4C43-8C08-9042385561C2} 8 | Exe 9 | Properties 10 | Questions 11 | Questions 12 | v4.0 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # csharp-interview-questions 2 | A collection of frequently asked C# programming questions in technical interviews. 3 | In this article we will learn about some of the frequently asked C# programming questions in technical interviews. 4 | 5 | # Read the full article at 6 | http://ankitsharmablogs.com/csharp-coding-questions-for-technical-interviews/ 7 | -------------------------------------------------------------------------------- /bin/Debug/Questions.vshost.exe.manifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /obj/Debug/Questions.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | c:\users\ankit sharma\documents\visual studio 2015\Projects\Questions\Questions\bin\Debug\Questions.exe 2 | c:\users\ankit sharma\documents\visual studio 2015\Projects\Questions\Questions\bin\Debug\Questions.pdb 3 | c:\users\ankit sharma\documents\visual studio 2015\Projects\Questions\Questions\obj\Debug\Questions.csprojResolveAssemblyReference.cache 4 | c:\users\ankit sharma\documents\visual studio 2015\Projects\Questions\Questions\obj\Debug\Questions.exe 5 | c:\users\ankit sharma\documents\visual studio 2015\Projects\Questions\Questions\obj\Debug\Questions.pdb 6 | --------------------------------------------------------------------------------