Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
public int FindDuplicate(int[] nums) { int slow = nums[0], fast = nums[0]; do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow != fast); fast = nums[0]; while (slow != fast) { slow = nums[slow]; fast = nums[fa…
int MinCutPalindromePartition(string s) { int n = s.Length; bool[,] dp = new bool[n, n]; int[] cuts = new int[n]; for (int i = 0; i < n; i++) { int minCuts = i; for (int j = 0; j <= i; j++) { if (s[j] == s[i] &…
bool PrintAncestors(TreeNode root, int target) { if (root == null) return false; if (root.val == target) return true; if (PrintAncestors(root.left, target) || PrintAncestors(root.right, target)) { Console.Write(root.val…
Answer: 1 : -1; return candidate; Explanation: Boyer-Moore Voting Algorithm tracks majority element by counting net votes. Follow on: What interviewers expect A clear definition tied to Coding in C# Coding Interview proj…
Answer: x * temp * temp : (temp * temp) / x; Explanation: Recursive fast power divides exponent by 2 to reduce complexity to O(log n). What interviewers expect A clear definition tied to Coding in C# Coding Interview pro…
Answer: 1 : -1; return candidate; Explanation: Maintain a candidate and count; majority element survives this cancellation. What interviewers expect A clear definition tied to Coding in C# Coding Interview projects Trade…
int MaxSubArray(int[] nums) { int maxSoFar = nums[0]; int maxEndingHere = nums[0]; for (int i = 1; i < nums.Length; i++) { maxEndingHere = Math.Max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.Max(maxSoFar, maxE…
public sealed class Singleton { private static readonly object lockObj = new object(); private static Singleton instance; private Singleton() { } public static Singleton Instance { get { if (instance == null) { lock (loc…
Answer: rray.Copy(nums, dp, n); for (int i = 1; i &lt; n; i++) { for (int j = 0; j &lt; i; j++) { Follow on: if (nums[i] &gt; nums[j]) dp[i] = Math.Max(dp[i], dp[j] + nums[i]); } } return dp.Max(); } What int…
bool IsValidBST(TreeNode root) { return Validate(root, null, null); } Follow on: bool Validate(TreeNode node, int? min, int? max) { if (node == null) return true; if ((min != null && node.val <= min) || (max !…
public int LengthOfLongestSubstringTwoDistinct(string s) { int left = 0, right = 0, maxLen = 0; Dictionary<char, int> map = new Dictionary<char, int>(); while (right < s.Length) { Follow on: char c = s[rig…
int MaxSumIncreasingSubsequence(int[] nums) { int n = nums.Length; int[] dp = new int[n]; Array.Copy(nums, dp, n); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { Follow on: if (nums[i] > nums[j]) dp…
min, int? max) { if (node == null) return true; if ((min != null && node.val <= min) || (max != null && node.val >= max)) return false; return Validate(node.left, min, node.val) && Validate(…
void RotateMatrix(int[][] matrix) { int n = matrix.Length; // Transpose for (int i = 0; i < n; i++) for (int j = i; j < n; j++) (matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j]); // Reverse each row for (…
Answer: int CountOnes(int n) { int count = 0; while (n != 0) { n &amp;= (n - 1); // drops the lowest set bit count++; } return count; } Explanation: Brian Kernighan’s algorithm removes one set bit per iteration. What…
int KthSmallest(int[][] matrix, int k) { int n = matrix.Length; int low = matrix[0][0], high = matrix[n - 1][n - 1]; while (low < high) { Follow on: int mid = low + (high - low) / 2; int count = 0, j = n - 1; for (int…
int RodCutting(int[] prices, int n) { int[] dp = new int[n + 1]; dp[0] = 0; Follow on: for (int i = 1; i <= n; i++) { int maxVal = int.MinValue; for (int j = 0; j < i; j++) { maxVal = Math.Max(maxVal, prices[j] + d…
ctualSum += num; int missing = expectedSum - actualSum; What interviewers expect A clear definition tied to Coding Scenarios in C# Coding Interview projects Trade-offs (performance, maintainability, security, cost) When…
double Calculate(double a, double b, char op) { return op switch { '+' => a + b, Follow on: '-' => a - b, '*' => a * b, '/' => b != 0 ? a / b : throw new DivideByZeroException(), _ => throw new ArgumentExc…
public bool IsBalanced(string s) { Stack<char> stack = new Stack<char>(); Dictionary<char, char> pairs = new Dictionary<char, char> { {')', '('}, {']', '['}, {'}', '{'} }; foreach (char c in s) {…
List<string> WordBreak(string s, IList<string> wordDict) { var wordSet = new HashSet<string>(wordDict); var memo = new Dictionary<int, List<string>>(); return DFS(0); List<string> DFS(…
int WidthOfBinaryTree(TreeNode root) { if (root == null) return 0; int maxWidth = 0; Queue<(TreeNode node, int idx)> queue = new Queue<(TreeNode, int)>(); queue.Enqueue((root, 0)); while (queue.Count > 0)…
bool SolveSudoku(char[][] board) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Follow on: if (board[i][j] == '.') { for (char c = '1'; c <= '9'; c++) { if (IsValid(board, i, j, c)) { board[i][j]…
Answer: a / b : throw new DivideByZeroException(), _ =&gt; throw new ArgumentException("Invalid operator"), Explanation: Simple switch statement performing basic arithmetic, with divide-by-zero check. What interviewe…
void SortColors(int[] nums) { int low = 0, mid = 0, high = nums.Length - 1; while (mid <= high) { if (nums[mid] == 0) (nums[low++], nums[mid++]) = (nums[mid], nums[low]); else if (nums[mid] == 1) mid++; else (nums[mid…
C# Coding Interview C# Programming Tutorial · Coding
public int FindDuplicate(int[] nums) {
int slow = nums[0], fast = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);
fast = nums[0];
while (slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
Explanation:
Floyd’s Tortoise and Hare cycle detection, treating values as pointers.
C# Coding Interview C# Programming Tutorial · Coding
int MinCutPalindromePartition(string s) {
int n = s.Length;
bool[,] dp = new bool[n, n];
int[] cuts = new int[n];
for (int i = 0; i < n; i++) {
int minCuts = i;
for (int j = 0; j <= i; j++) {
if (s[j] == s[i] && (i - j < 2 || dp[j + 1, i - 1])) {
dp[j, i] = true;
minCuts = j == 0 ? 0 : Math.Min(minCuts, cuts[j - 1]
+ 1);
}
}
cuts[i] = minCuts;
}
return cuts[n - 1];
}C# Coding Interview C# Programming Tutorial · Coding
bool PrintAncestors(TreeNode root, int target) {
if (root == null) return false;
if (root.val == target) return true;
if (PrintAncestors(root.left, target) ||
PrintAncestors(root.right, target)) {
Console.Write(root.val + " ");
return true;
}
return false;
}
Explanation:
Recursive check if target is in subtree; print current node on path back if yes.
C# Coding Interview C# Programming Tutorial · Coding
Answer: 1 : -1; return candidate; Explanation: Boyer-Moore Voting Algorithm tracks majority element by counting net votes. Follow on:
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
Answer: x * temp * temp : (temp * temp) / x; Explanation: Recursive fast power divides exponent by 2 to reduce complexity to O(log n).
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
Answer: 1 : -1; return candidate; Explanation: Maintain a candidate and count; majority element survives this cancellation.
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
int MaxSubArray(int[] nums)
{
int maxSoFar = nums[0];
int maxEndingHere = nums[0];
for (int i = 1; i < nums.Length; i++)
{
maxEndingHere = Math.Max(nums[i], maxEndingHere + nums[i]);
maxSoFar = Math.Max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
Explanation:
Track max subarray ending at current position; update global max.
C# Coding Interview C# Programming Tutorial · Coding Scenarios
public sealed class Singleton
{
private static readonly object lockObj = new object();
private static Singleton instance;
private Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (lockObj)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}C# Coding Interview C# Programming Tutorial · Coding
Answer: rray.Copy(nums, dp, n); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { Follow on: if (nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + nums[i]); } } return dp.Max(); }
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
bool IsValidBST(TreeNode root) {
return Validate(root, null, null);
}
Follow on:
bool Validate(TreeNode node, int? min, int? max) {
if (node == null) return true;
if ((min != null && node.val <= min) || (max != null && node.val
>= max)) return false;
return Validate(node.left, min, node.val) &&
Validate(node.right, node.val, max);
}
Explanation:
Pass down min and max bounds for subtree values; node must be in (min, max) range.
C# Coding Interview C# Programming Tutorial · Coding
public int LengthOfLongestSubstringTwoDistinct(string s) {
int left = 0, right = 0, maxLen = 0;
Dictionary<char, int> map = new Dictionary<char, int>();
while (right < s.Length) {
Follow on:
char c = s[right];
map[c] = right;
if (map.Count > 2) {
int delIndex = map.Values.Min();
map.Remove(s[delIndex]);
left = delIndex + 1;
}
maxLen = Math.Max(maxLen, right - left + 1);
right++;
}
return maxLen;
}
Explanation:
Sliding window with hashmap to track indices of distinct chars, remove the leftmost when
>2.
C# Coding Interview C# Programming Tutorial · Coding
int MaxSumIncreasingSubsequence(int[] nums) {
int n = nums.Length;
int[] dp = new int[n];
Array.Copy(nums, dp, n);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
Follow on:
if (nums[i] > nums[j])
dp[i] = Math.Max(dp[i], dp[j] + nums[i]);
return dp.Max();
C# Coding Interview C# Programming Tutorial · Coding
min, int? max) {
if (node == null) return true;
if ((min != null && node.val <= min) || (max != null && node.val
>= max)) return false;
return Validate(node.left, min, node.val) &&
Validate(node.right, node.val, max);
Explanation:
Pass down min and max bounds for subtree values; node must be in (min, max) range.
C# Coding Interview C# Programming Tutorial · Coding
void RotateMatrix(int[][] matrix)
{
int n = matrix.Length;
// Transpose
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
(matrix[i][j], matrix[j][i]) = (matrix[j][i],
matrix[i][j]);
// Reverse each row
for (int i = 0; i < n; i++)
{
int left = 0, right = n - 1;
while (left < right)
{
(matrix[i][left], matrix[i][right]) = (matrix[i][right],
matrix[i][left]);
left++;
right--;
}
}
}
Explanation:
Transpose matrix and then reverse each row to rotate clockwise by 90°.
C# Coding Interview C# Programming Tutorial · Coding
Answer: int CountOnes(int n) { int count = 0; while (n != 0) { n &= (n - 1); // drops the lowest set bit count++; } return count; } Explanation: Brian Kernighan’s algorithm removes one set bit per iteration.
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
int KthSmallest(int[][] matrix, int k)
{
int n = matrix.Length;
int low = matrix[0][0], high = matrix[n - 1][n - 1];
while (low < high)
{
Follow on:
int mid = low + (high - low) / 2;
int count = 0, j = n - 1;
for (int i = 0; i < n; i++)
{
while (j >= 0 && matrix[i][j] > mid)
j--;
count += (j + 1);
}
if (count < k)
low = mid + 1;
else
high = mid;
}
return low;
}
Explanation:
Binary search on values, count how many elements ≤ mid using matrix’s sorted rows/cols.
C# Coding Interview C# Programming Tutorial · Coding
int RodCutting(int[] prices, int n)
{
int[] dp = new int[n + 1];
dp[0] = 0;
Follow on:
for (int i = 1; i <= n; i++)
{
int maxVal = int.MinValue;
for (int j = 0; j < i; j++)
{
maxVal = Math.Max(maxVal, prices[j] + dp[i - j - 1]);
}
dp[i] = maxVal;
}
return dp[n];
}
Explanation:
Max revenue by cutting rod into pieces of various lengths.
C# Coding Interview C# Programming Tutorial · Coding Scenarios
ctualSum += num; int missing = expectedSum - actualSum;
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
double Calculate(double a, double b, char op)
{
return op switch
{
'+' => a + b,
Follow on:
'-' => a - b,
'*' => a * b,
'/' => b != 0 ? a / b : throw new DivideByZeroException(),
_ => throw new ArgumentException("Invalid operator"),
};
}
Explanation:
Simple switch statement performing basic arithmetic, with divide-by-zero check.
C# Coding Interview C# Programming Tutorial · Coding
public bool IsBalanced(string s) {
Stack<char> stack = new Stack<char>();
Dictionary<char, char> pairs = new Dictionary<char, char> {
{')', '('}, {']', '['}, {'}', '{'}
};
foreach (char c in s) {
if ("([{".Contains(c))
stack.Push(c);
else if (")]}".Contains(c)) {
if (stack.Count == 0 || stack.Pop() != pairs[c])
return false;
}
}
return stack.Count == 0;
}
Follow on:
Explanation:
Use a stack to match opening and closing brackets properly.
C# Coding Interview C# Programming Tutorial · Coding
List<string> WordBreak(string s, IList<string> wordDict) {
var wordSet = new HashSet<string>(wordDict);
var memo = new Dictionary<int, List<string>>();
return DFS(0);
List<string> DFS(int start) {
if (memo.ContainsKey(start)) return memo[start];
var res = new List<string>();
if (start == s.Length) {
res.Add("");
return res;
}
for (int end = start + 1; end <= s.Length; end++) {
string word = s.Substring(start, end - start);
if (wordSet.Contains(word)) {
foreach (var sub in DFS(end)) {
string space = sub.Length == 0 ? "" : " ";
res.Add(word + space + sub);
}
}
}
memo[start] = res;
return res;
}
}
Follow on:
C# Coding Interview C# Programming Tutorial · Coding
int WidthOfBinaryTree(TreeNode root) {
if (root == null) return 0;
int maxWidth = 0;
Queue<(TreeNode node, int idx)> queue = new Queue<(TreeNode,
int)>();
queue.Enqueue((root, 0));
while (queue.Count > 0) {
int size = queue.Count;
int start = queue.Peek().idx;
int end = start;
for (int i = 0; i < size; i++) {
var (node, idx) = queue.Dequeue();
end = idx;
if (node.left != null)
queue.Enqueue((node.left, 2 * idx + 1));
if (node.right != null)
queue.Enqueue((node.right, 2 * idx + 2));
maxWidth = Math.Max(maxWidth, end - start + 1);
Follow on:
return maxWidth;
Explanation:
Assign index to each node as if in a complete tree; width is max difference of indices per
level.
C# Coding Interview C# Programming Tutorial · Coding
bool SolveSudoku(char[][] board)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Follow on:
if (board[i][j] == '.')
{
for (char c = '1'; c <= '9'; c++)
{
if (IsValid(board, i, j, c))
{
board[i][j] = c;
if (SolveSudoku(board))
return true;
else
board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
bool IsValid(char[][] board, int row, int col, char c)
{
for (int i = 0; i < 9; i++)
{
if (board[row][i] == c) return false;
if (board[i][col] == c) return false;
if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] ==
c) return false;
}
return true;
}
Explanation:
Backtracking tries digits 1-9 in empty cells, validating constraints.
C# Coding Interview C# Programming Tutorial · Coding
Answer: a / b : throw new DivideByZeroException(), _ => throw new ArgumentException("Invalid operator"), Explanation: Simple switch statement performing basic arithmetic, with divide-by-zero check.
In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.
Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.
C# Coding Interview C# Programming Tutorial · Coding
void SortColors(int[] nums)
{
int low = 0, mid = 0, high = nums.Length - 1;
while (mid <= high)
{
if (nums[mid] == 0)
(nums[low++], nums[mid++]) = (nums[mid], nums[low]);
else if (nums[mid] == 1)
mid++;
else
(nums[mid], nums[high--]) = (nums[high], nums[mid]);
}
}
Follow on:
Explanation:
Partition array into three parts in one pass using three pointers.