C# Coding Interview Questions – Strings and Arrays
1. How do you reverse a string without using built-in reverse methods?
Explanation: Reverse a string by reading its characters from the last position to the first position.
Example:
string input = "Hello";
string reversed = "";
for (int i = input.Length - 1; i >= 0; i--)
{
reversed += input[i];
}
Console.WriteLine(reversed);
Output:
olleH
How it works:
Input: H e l l o
Index: 0 1 2 3 4
Read: 4 -> 3 -> 2 -> 1 -> 0
Output: o l l e H
Key Points:
-
Length - 1identifies the last character. -
The loop runs backward.
-
Time complexity: O(n²) with repeated string concatenation in this implementation.
-
For large strings, use
char[]orStringBuilderfor O(n) performance.
2. How do you find duplicate elements in an array? 🔴
Explanation: Duplicate elements are values that appear more than once in an array.
Example:
int[] numbers = { 10, 20, 30, 20, 40, 10 };
HashSet<int> seen = new HashSet<int>();
HashSet<int> duplicates = new HashSet<int>();
foreach (int number in numbers)
{
if (!seen.Add(number))
{
duplicates.Add(number);
}
}
foreach (int number in duplicates)
{
Console.WriteLine(number);
}
Output:
20
10
How it works:
-
seenstores numbers already encountered. -
seen.Add()returnsfalsewhen a number already exists. -
Duplicate numbers are added to
duplicates. -
HashSetprevents duplicate results.
Key Points:
-
HashSet<T>stores unique elements. -
Add()returnstruefor a new element. -
Average time complexity: O(n).
-
Additional space complexity: O(n).
3. How do you find the second-highest number in an array? 🔴
Explanation: Find the second-largest distinct value without sorting the array.
Example:
int[] numbers = { 10, 50, 30, 40, 20 };
int? highest = null;
int? secondHighest = null;
foreach (int number in numbers)
{
if (highest == null || number > highest)
{
secondHighest = highest;
highest = number;
}
else if (number < highest &&
(secondHighest == null || number > secondHighest))
{
secondHighest = number;
}
}
Console.WriteLine(secondHighest?.ToString() ?? "Not found");
Output:
40
How it works:
Array: 10, 50, 30, 40, 20
Highest = 50
SecondHighest = 40
Result = 40
Key Points:
-
No sorting is required.
-
Uses a single loop.
-
Time complexity: O(n).
-
Space complexity: O(1).
-
Duplicate maximum values are ignored.
-
Returns
"Not found"if there are fewer than two distinct values.
4. How do you remove duplicate characters from a string? 🔴
Explanation: Remove repeated characters while preserving the order of their first appearance.
Example:
string input = "programming";
string result = "";
HashSet<char> seen = new HashSet<char>();
foreach (char ch in input)
{
if (seen.Add(ch))
{
result += ch;
}
}
Console.WriteLine(result);
Output:
progamin
How it works:
Input: p r o g r a m m i n g
Remove repeated:
r, m, g
Output: p r o g a m i n
Key Points:
-
HashSet<char>identifies repeated characters. -
Maintains the original character order.
-
Character comparison is case-sensitive (
Aandaare different). -
For large strings, use
StringBuilderinstead of repeated concatenation.
5. How do you check whether a string is a palindrome? 🔴
Explanation: A palindrome is a string that reads the same forward and backward.
Examples: madam, level, radar.
Example:
string input = "madam";
int left = 0;
int right = input.Length - 1;
bool isPalindrome = true;
while (left < right)
{
if (input[left] != input[right])
{
isPalindrome = false;
break;
}
left++;
right--;
}
Console.WriteLine(isPalindrome);
Output:
True
How it works:
Input: m a d a m
0 1 2 3 4
Step 1: m == m -> True
Step 2: a == a -> True
Result: Palindrome
Key Points:
-
Uses the two-pointer technique.
-
Compares characters from both ends.
-
Stops immediately when characters differ.
-
Time complexity: O(n).
-
Space complexity: O(1).
-
This example is case-sensitive and includes spaces in comparisons.
Quick Interview Revision
|
Question |
Technique |
Time Complexity |
|---|---|---|
|
Reverse string |
Backward loop |
O(n²) with concatenation |
|
Find duplicates |
|
O(n) average |
|
Second-highest number |
Single loop |
O(n) |
|
Remove duplicate characters |
|
O(n²) with concatenation |
|
Check palindrome |
Two pointers |
O(n) |
Interview Tip: For coding interviews, explain the approach first, write the code, and finally mention time complexity and space complexity.