Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
public int[] MaxSlidingWindow(int[] nums, int k) { if (nums == null || k <= 0) return new int[0]; int n = nums.Length; int[] result = new int[n - k + 1]; LinkedList<int> deque = new LinkedList<int>(); // s…
l1.val : 0; int y = (l2 != null) ? l2.val : 0; int sum = x + y + carry; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (l1 != null) l1 = l1.next; if (l2 != null) l2 = l2.next; Follow on: retur…
Answer: bool IsRotation(string s1, string s2) { if (s1.Length != s2.Length) return false; string doubled = s1 + s1; return doubled.Contains(s2); } Follow on: Explanation: If s2 is rotation of s1, it must be substring of…
int NumIslands(char[][] grid) { if (grid == null || grid.Length == 0) return 0; int count = 0; for (int i = 0; i < grid.Length; i++) { for (int j = 0; j < grid[0].Length; j++) { if (grid[i][j] == '1') Follow on: {…
Answer: int SumOfDigits(int n) { int sum = 0; n = Math.Abs(n); while (n &gt; 0) { sum += n % 10; n /= 10; } return sum; } Explanation: Extract digits using modulo 10 and add. What interviewers expect A clear definiti…
Follow on: int BinarySearch(int[] arr, int target) { int low = 0, high = arr.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) low =…
mount) int CoinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; rray.Fill(dp, amount + 1); dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (coin <= i) dp[i] = Math.M…
string sentence = "dotnet is great dotnet is powerful"; string[] words = sentence.Split(' '); Dictionary<string, int> map = new Dictionary<string, int>(); foreach (string word in words) map[word] = map.Contai…
public uint ReverseBits(uint n) { uint result = 0; for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } Explanation: Iteratively take least significant bit of n,…
public class TrieNode { public TrieNode[] Children = new TrieNode[26]; public bool IsEnd = false; } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void Insert(string word) { Tr…
Answer: public int TrailingZeroes(int n) { int count = 0; while (n &gt; 0) { n /= 5; count += n; } return count; } Explanation: Count factors of 5 in factorial since 2s are plentiful, trailing zeros depend on 5s. Wha…
public int[] SearchRange(int[] nums, int target) { int left = FindBoundary(nums, target, true); int right = FindBoundary(nums, target, false); return new int[] { left, right }; } private int FindBoundary(int[] nums, int…
int EditDistance(string word1, string word2) { int m = word1.Length, n = word2.Length; int[,] dp = new int[m + 1, n + 1]; for (int i = 0; i <= m; i++) dp[i, 0] = i; for (int j = 0; j <= n; j++) dp[0, j] = j; for (i…
void PrintNodesAtLevel(Dictionary<int, List<int>> graph, int start, int targetLevel) { var visited = new HashSet<int>(); var queue = new Queue<(int node, int level)>(); Follow on: queue.Enqueue((s…
List<List<int>> LevelOrderBottom(TreeNode root) { var res = new List<List<int>>(); if (root == null) return res; Queue<TreeNode> queue = new Queue<TreeNode>(); queue.Enqueue(root); whi…
public class MultiLevelNode { public int val; public MultiLevelNode next; public MultiLevelNode child; public MultiLevelNode(int x) { val = x; next = null; child = null; } } MultiLevelNode Flatten(MultiLevelNode head) {…
ll Characters of String t string MinWindow(string s, string t) { if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return ""; Dictionary<char, int> dictT = new Dictionary<char, int>(); foreach (char c i…
rray int LongestConsecutive(int[] nums) { HashSet<int> set = new HashSet<int>(nums); int longest = 0; foreach (int num in set) { if (!set.Contains(num - 1)) Follow on: { int currentNum = num; int length = 1;…
Answer: int TrailingZeroes(int n) { int count = 0; for (int i = 5; i &lt;= n; i *= 5) { count += n / i; } return count; } Explanation: Trailing zeros come from factors of 10 = 2 × 5, but 2s are plenty, count 5s. What…
(Others Appear Twice) int SingleNonDuplicate(int[] nums) { int low = 0, high = nums.Length - 1; while (low < high) { int mid = low + (high - low) / 2; if (mid % 2 == 1) mid--; // ensure mid is even if (nums[mid] == nu…
bool WordBreak(string s, HashSet<string> wordDict) { bool[] dp = new bool[s.Length + 1]; dp[0] = true; for (int i = 1; i <= s.Length; i++) { for (int j = 0; j < i; j++) { if (dp[j] && wordDict.Contain…
string input = "programming"; Dictionary<char, int> map = new Dictionary<char, int>(); foreach (char c in input) map[c] = map.ContainsKey(c) ? map[c] + 1 : 1; foreach (var item in map) { if (item.Value > 1…
int MajorityElement(int[] nums) { int count = 0, candidate = 0; foreach (var num in nums) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; } return candidate; } Explanation: Boyer-Moore Voting Alg…
double Power(double x, int n) Follow on: { if (n == 0) return 1; double temp = Power(x, n / 2); if (n % 2 == 0) return temp * temp; else return (n > 0) ? x * temp * temp : (temp * temp) / x; } Explanation: Recursive f…
lgorithm) int MajorityElement(int[] nums) { int count = 0, candidate = 0; foreach (var num in nums) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; } return candidate; } Explanation: Maintain a c…
C# Coding Interview C# Programming Tutorial · Coding
public int[] MaxSlidingWindow(int[] nums, int k) {
if (nums == null || k <= 0) return new int[0];
int n = nums.Length;
int[] result = new int[n - k + 1];
LinkedList<int> deque = new LinkedList<int>(); // store indices
for (int i = 0; i < n; i++) {
// Remove indices out of window
if (deque.Count > 0 && deque.First.Value <= i - k)
Follow on:
deque.RemoveFirst();
// Remove smaller values from the back
while (deque.Count > 0 && nums[deque.Last.Value] < nums[i])
deque.RemoveLast();
deque.AddLast(i);
if (i >= k - 1)
result[i - k + 1] = nums[deque.First.Value];
return result;
Explanation:
Use a deque to keep indexes of useful elements in current window, ensuring the front is
always max.
C# Coding Interview C# Programming Tutorial · Coding
l1.val : 0;
int y = (l2 != null) ? l2.val : 0;
int sum = x + y + carry;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
Follow on:
return dummy.next;
Explanation:
Add digit by digit with carry, creating new nodes for the result.
C# Coding Interview C# Programming Tutorial · Coding
Answer: bool IsRotation(string s1, string s2) { if (s1.Length != s2.Length) return false; string doubled = s1 + s1; return doubled.Contains(s2); } Follow on: Explanation: If s2 is rotation of s1, it must be substring of s1+s1.
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 NumIslands(char[][] grid)
{
if (grid == null || grid.Length == 0) return 0;
int count = 0;
for (int i = 0; i < grid.Length; i++)
{
for (int j = 0; j < grid[0].Length; j++)
{
if (grid[i][j] == '1')
Follow on:
{
DFS(grid, i, j);
count++;
}
}
}
return count;
}
void DFS(char[][] grid, int i, int j)
{
if (i < 0 || j < 0 || i >= grid.Length || j >= grid[0].Length ||
grid[i][j] == '0')
return;
grid[i][j] = '0'; // Mark visited
DFS(grid, i + 1, j);
DFS(grid, i - 1, j);
DFS(grid, i, j + 1);
DFS(grid, i, j - 1);
}
Explanation:
Use DFS to mark all connected land cells, count islands by visiting unvisited lands.
C# Coding Interview C# Programming Tutorial · Coding
Answer: int SumOfDigits(int n) { int sum = 0; n = Math.Abs(n); while (n > 0) { sum += n % 10; n /= 10; } return sum; } Explanation: Extract digits using modulo 10 and add.
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
Follow on:
int BinarySearch(int[] arr, int target)
{
int low = 0, high = arr.Length - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
Explanation:
Standard binary search to find target’s index or -1 if not found.
C# Coding Interview C# Programming Tutorial · Coding
mount)
int CoinChange(int[] coins, int amount)
{
int[] dp = new int[amount + 1];
rray.Fill(dp, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++)
{
foreach (int coin in coins)
{
if (coin <= i)
dp[i] = Math.Min(dp[i], 1 + dp[i - coin]);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
Explanation:
Bottom-up DP: min coins needed for all amounts up to target.
Follow on:
C# Coding Interview C# Programming Tutorial · Coding Scenarios
string sentence = "dotnet is great dotnet is powerful";
string[] words = sentence.Split(' ');
Dictionary<string, int> map = new Dictionary<string, int>();
foreach (string word in words)
map[word] = map.ContainsKey(word) ? map[word] + 1 : 1;C# Coding Interview C# Programming Tutorial · Coding
public uint ReverseBits(uint n) {
uint result = 0;
for (int i = 0; i < 32; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
Explanation:
Iteratively take least significant bit of n, add it to result’s LSB, shift both accordingly.
Follow on:
Follow on:
C# Coding Interview C# Programming Tutorial · Coding
public class TrieNode {
public TrieNode[] Children = new TrieNode[26];
public bool IsEnd = false;
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
int idx = c - 'a';
if (node.Children[idx] == null)
node.Children[idx] = new TrieNode();
node = node.Children[idx];
}
node.IsEnd = true;
}
public bool Search(string word) {
TrieNode node = SearchNode(word);
return node != null && node.IsEnd;
}
public bool StartsWith(string prefix) {
return SearchNode(prefix) != null;
}
private TrieNode SearchNode(string word) {
TrieNode node = root;
foreach (char c in word) {
int idx = c - 'a';
if (node.Children[idx] == null) return null;
Follow on:
node = node.Children[idx];
}
return node;
}
}
Explanation:
Standard trie with insert, search, and prefix checking.
C# Coding Interview C# Programming Tutorial · Coding
Answer: public int TrailingZeroes(int n) { int count = 0; while (n > 0) { n /= 5; count += n; } return count; } Explanation: Count factors of 5 in factorial since 2s are plentiful, trailing zeros depend on 5s.
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
public int[] SearchRange(int[] nums, int target) {
int left = FindBoundary(nums, target, true);
int right = FindBoundary(nums, target, false);
return new int[] { left, right };
}
private int FindBoundary(int[] nums, int target, bool findFirst) {
Follow on:
int left = 0, right = nums.Length - 1;
int boundary = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
boundary = mid;
if (findFirst)
right = mid - 1;
else
left = mid + 1;
}
else if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return boundary;
}
Explanation:
Binary search twice — once to find first occurrence and once for last occurrence.
C# Coding Interview C# Programming Tutorial · Coding
int EditDistance(string word1, string word2) {
int m = word1.Length, n = word2.Length;
int[,] dp = new int[m + 1, n + 1];
for (int i = 0; i <= m; i++) dp[i, 0] = i;
for (int j = 0; j <= n; j++) dp[0, j] = j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (word1[i - 1] == word2[j - 1])
dp[i, j] = dp[i - 1, j - 1];
else
Follow on:
dp[i, j] = 1 + Math.Min(dp[i - 1, j - 1],
Math.Min(dp[i - 1, j], dp[i, j - 1]));
}
}
return dp[m, n];
}C# Coding Interview C# Programming Tutorial · Coding
void PrintNodesAtLevel(Dictionary<int, List<int>> graph, int start,
int targetLevel) {
var visited = new HashSet<int>();
var queue = new Queue<(int node, int level)>();
Follow on:
queue.Enqueue((start, 0));
visited.Add(start);
while (queue.Count > 0) {
var (node, level) = queue.Dequeue();
if (level == targetLevel) {
Console.WriteLine(node);
}
if (level > targetLevel) break;
foreach (var neighbor in graph[node]) {
if (!visited.Contains(neighbor)) {
visited.Add(neighbor);
queue.Enqueue((neighbor, level + 1));
}
}
}
}
Explanation:
BFS traversal with level tracking; print nodes at the requested level.
C# Coding Interview C# Programming Tutorial · Coding
List<List<int>> LevelOrderBottom(TreeNode root) {
var res = new List<List<int>>();
if (root == null) return res;
Queue<TreeNode> queue = new Queue<TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0) {
int size = queue.Count;
var level = new List<int>();
Follow on:
for (int i = 0; i < size; i++) {
TreeNode node = queue.Dequeue();
level.Add(node.val);
if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}
res.Insert(0, level); // prepend to get reverse order
}
return res;
}
Explanation:
Perform normal BFS, insert each level at front of result list for reversed order.
C# Coding Interview C# Programming Tutorial · Coding
public class MultiLevelNode {
public int val;
public MultiLevelNode next;
public MultiLevelNode child;
public MultiLevelNode(int x) { val = x; next = null; child =
null; }
}
MultiLevelNode Flatten(MultiLevelNode head) {
if (head == null) return null;
MultiLevelNode dummy = new MultiLevelNode(0);
MultiLevelNode prev = dummy;
Stack<MultiLevelNode> stack = new Stack<MultiLevelNode>();
stack.Push(head);
while (stack.Count > 0) {
var curr = stack.Pop();
prev.next = curr;
curr.child = null; // remove child pointer after flattening
prev = curr;
if (curr.next != null) stack.Push(curr.next);
if (curr.child != null) stack.Push(curr.child);
}
return dummy.next;
}
Follow on:
Explanation:
Use a stack to perform DFS; attach nodes and remove child pointers.
C# Coding Interview C# Programming Tutorial · Coding
ll Characters of String t
string MinWindow(string s, string t)
{
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return
"";
Dictionary<char, int> dictT = new Dictionary<char, int>();
foreach (char c in t)
dictT[c] = dictT.ContainsKey(c) ? dictT[c] + 1 : 1;
int required = dictT.Count;
int formed = 0;
Dictionary<char, int> windowCounts = new Dictionary<char,
int>();
int left = 0, right = 0;
int minLen = int.MaxValue, minLeft = 0;
while (right < s.Length)
{
char c = s[right];
windowCounts[c] = windowCounts.ContainsKey(c) ?
windowCounts[c] + 1 : 1;
if (dictT.ContainsKey(c) && windowCounts[c] == dictT[c])
formed++;
while (left <= right && formed == required)
{
if (right - left + 1 < minLen)
{
minLen = right - left + 1;
Follow on:
minLeft = left;
}
char leftChar = s[left];
windowCounts[leftChar]--;
if (dictT.ContainsKey(leftChar) &&
windowCounts[leftChar] < dictT[leftChar])
formed--;
left++;
}
right++;
}
return minLen == int.MaxValue ? "" : s.Substring(minLeft,
minLen);
}
Explanation:
Sliding window with two pointers keeps track of counts of chars matching the target.
C# Coding Interview C# Programming Tutorial · Coding
rray
int LongestConsecutive(int[] nums)
{
HashSet<int> set = new HashSet<int>(nums);
int longest = 0;
foreach (int num in set)
{
if (!set.Contains(num - 1))
Follow on:
{
int currentNum = num;
int length = 1;
while (set.Contains(currentNum + 1))
{
currentNum++;
length++;
}
longest = Math.Max(longest, length);
}
}
return longest;
}
Explanation:
Check only starts of sequences, count consecutive numbers using HashSet for O(n).
C# Coding Interview C# Programming Tutorial · Coding
Answer: int TrailingZeroes(int n) { int count = 0; for (int i = 5; i <= n; i *= 5) { count += n / i; } return count; } Explanation: Trailing zeros come from factors of 10 = 2 × 5, but 2s are plenty, count 5s.
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
(Others Appear Twice)
int SingleNonDuplicate(int[] nums)
{
int low = 0, high = nums.Length - 1;
while (low < high)
{
int mid = low + (high - low) / 2;
if (mid % 2 == 1) mid--; // ensure mid is even
if (nums[mid] == nums[mid + 1])
low = mid + 2;
else
high = mid;
Follow on:
}
return nums[low];
}
Explanation:
Pairs appear consecutively; use binary search on even indices to find mismatch.
C# Coding Interview C# Programming Tutorial · Coding
bool WordBreak(string s, HashSet<string> wordDict)
{
bool[] dp = new bool[s.Length + 1];
dp[0] = true;
for (int i = 1; i <= s.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (dp[j] && wordDict.Contains(s.Substring(j, i - j)))
{
dp[i] = true;
break;
}
}
}
return dp[s.Length];
}
Explanation:
DP to check if substring can be segmented using dictionary words.
Follow on:
C# Coding Interview C# Programming Tutorial · Coding Scenarios
string input = "programming";
Dictionary<char, int> map = new Dictionary<char, int>();
foreach (char c in input)
map[c] = map.ContainsKey(c) ? map[c] + 1 : 1;
foreach (var item in map)
{
if (item.Value > 1)
Console.WriteLine(item.Key);
}C# Coding Interview C# Programming Tutorial · Coding
int MajorityElement(int[] nums)
{
int count = 0, candidate = 0;
foreach (var num in nums)
{
if (count == 0)
candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
Explanation:
Boyer-Moore Voting Algorithm tracks majority element by counting net votes.
Follow on:
C# Coding Interview C# Programming Tutorial · Coding
double Power(double x, int n)
Follow on:
{
if (n == 0) return 1;
double temp = Power(x, n / 2);
if (n % 2 == 0)
return temp * temp;
else
return (n > 0) ? x * temp * temp : (temp * temp) / x;
}
Explanation:
Recursive fast power divides exponent by 2 to reduce complexity to O(log n).
C# Coding Interview C# Programming Tutorial · Coding
lgorithm)
int MajorityElement(int[] nums)
{
int count = 0, candidate = 0;
foreach (var num in nums)
{
if (count == 0)
candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
Explanation:
Maintain a candidate and count; majority element survives this cancellation.