Interview Q&A

Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.

4608 total questions 4508 technical 100 career & HR 4272 from PDF library

Showing 926–950 of 4608

Career & HR topics

By tech stack

Popular tracks

Mid PDF
Convert a binary tree to a doubly linked list (in-order)?

Short answer: public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } TreeNode prev = null; TreeNode head = null; TreeNode ConvertToDLL(TreeNode root) { if (root == null…

Coding Read answer
Mid PDF
Implement a queue using stacks (efficient enqueue and dequeue)?

Short answer: public class MyQueue { private Stack<int> stackIn = new Stack<int>(); private Stack<int> stackOut = new Stack<int>(); // Enqueue: push into stackIn (O(1)) public void Enqueue(int x)…

Coding Read answer
Mid PDF
Detect a cycle in a linked list and return the node where the cycle?

Short answer: begins public class ListNode { Example code public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } ListNode DetectCycle(ListNode head) { if (head == null) return null; List…

Coding Read answer
Mid PDF
Count Inversions in an Array?

Short answer: int MergeSortAndCount(int[] arr, int[] temp, int left, int right) { int invCount = 0; if (right > left) { int mid = (right + left) / 2; invCount += MergeSortAndCount(arr, temp, left, mid); invCount += Me…

Coding Read answer
Mid PDF
Greatest Common Divisor (GCD) — Euclidean?

Short answer: Algorithm int GCD(int a, int b) { Follow on: while (b != 0) { Example code int temp = b; b = a % b; a = temp; } return a; } Explanation: Repeatedly replace (a, b) with (b, a mod b) until b is 0; then a is t…

Coding Read answer
Mid PDF
Quicksort?

Short answer: void QuickSort(int[] arr, int low, int high) { Example code if (low < high) Follow on: { int pi = Partition(arr, low, high); QuickSort(arr, low, pi - 1); QuickSort(arr, pi + 1, high); } } int Partition(i…

Coding Read answer
Mid PDF
Find the nth Fibonacci Number?

Short answer: int Fibonacci(int n) { if (n <= 1) return n; int a = 0, b = 1; for (int i = 2; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; } Explanation: Iterative DP approach; each Fibonacci number…

Coding Read answer
Mid PDF
Count the number of set bits (1s) in binary representation of an integer public int CountSetBits(int n) { int count = 0; while (n != 0) { count += n & 1; n >>= 1; } return count; } Explanation: Shift through each bit; add 1 to count if least significant bit is set.

Short answer: lternative using Brian Kernighan’s algorithm: public int CountSetBits(int n) { int count = 0; while (n != 0) { n &= (n - 1); // Drops the lowest set bit count++; } return count; } Follow on: lternat…

Coding Read answer
Mid PDF
Mergesort void MergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; MergeSort(arr, left, mid); MergeSort(arr, mid + 1, right); Follow on: Merge(arr, left, mid, right); } } void Merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2];

Short answer: rray.Copy(arr, left, L, 0, n1); rray.Copy(arr, mid + 1, R, 0, n2); int i = 0, j = 0, k = left; while (i &lt; n1 &amp;&amp; j &lt; n2) rr[k++] = (L[i] &lt;= R[j]) ? L[i++] : R[j++]; while (i &lt; n1) arr[k++…

Coding Read answer
Mid PDF
Count the number of 1s in the binary representation of a number?

Short answer: public 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 efficiently removes the lowest…

Coding Read answer
Mid PDF
Find the longest common prefix among a list of strings?

Short answer: public string LongestCommonPrefix(string[] strs) { if (strs == null || strs.Length == 0) return &quot;&quot;; for (int i = 0; i &lt; strs[0].Length; i++) { char c = strs[0][i]; for (int j = 1; j &lt; strs.L…

Coding Read answer
Mid PDF
Sort a nearly sorted array (each element at most k positions away)?

Short answer: public int[] SortNearlySorted(int[] nums, int k) { var result = new List&lt;int&gt;(); var minHeap = new SortedSet&lt;(int val, int index)&gt;(); for (int i = 0; i &lt; nums.Length; i++) { minHeap.Add((nums…

Coding Read answer
Mid PDF
Climbing Stairs (1 or 2 steps)?

Short answer: int ClimbStairs(int n) { if (n &lt;= 2) return n; int a = 1, b = 2; for (int i = 3; i &lt;= n; i++) { int c = a + b; a = b; b = c; } return b; } Example code int ClimbStairs(int n) { if (n &lt;= 2) return n…

Coding Read answer
Mid PDF
Number of connected components in undirected graph?

Short answer: int CountConnectedComponents(Dictionary&lt;int, List&lt;int&gt;&gt; graph) { var visited = new HashSet&lt;int&gt;(); int count = 0; Follow on: foreach (var node in graph.Keys) { if (!visited.Contains(node))…

Coding Read answer
Mid PDF
Find the vertical sum of a binary tree?

Short answer: void VerticalSum(TreeNode root, int hd, Dictionary&lt;int, int&gt; map) { if (root == null) return; VerticalSum(root.left, hd - 1, map); if (map.ContainsKey(hd)) map[hd] += root.val; else map[hd] = root.val…

Coding Read answer
Mid PDF
Evaluate an infix expression (with parentheses)?

Short answer: public int EvaluateInfix(string expression) { Stack&lt;int&gt; operands = new Stack&lt;int&gt;(); Stack&lt;char&gt; operators = new Stack&lt;char&gt;(); int i = 0; while (i &lt; expression.Length) { if (cha…

Coding Read answer
Mid PDF
Reverse a portion of a linked list (from position m to n)?

Short answer: ListNode ReverseBetween(ListNode head, int m, int n) { Example code if (head == null || m == n) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode prev = dummy; // Move prev to one b…

Coding Read answer
Mid PDF
Find the Longest Palindromic Substring?

Short answer: string LongestPalindrome(string s) { if (string.IsNullOrEmpty(s)) return &quot;&quot;; int start = 0, maxLen = 1; for (int i = 0; i &lt; s.Length; i++) { ExpandAroundCenter(s, i, i, ref start, ref maxLen);…

Coding Read answer
Mid PDF
Least Common Multiple (LCM)?

Short answer: int LCM(int a, int b) { return a / GCD(a, b) * b; } Explanation: LCM × GCD = product of the two numbers. Example code int LCM(int a, int b) { return a / GCD(a, b) * b; } Explanation: LCM × GCD = product of…

Coding Read answer
Mid PDF
Mergesort?

Short answer: void MergeSort(int[] arr, int left, int right) { Example code if (left &lt; right) { int mid = (left + right) / 2; MergeSort(arr, left, mid); MergeSort(arr, mid + 1, right); Follow on: Merge(arr, left, mid,…

Coding Read answer
Mid PDF
0/1 Knapsack Problem?

Short answer: int Knapsack(int[] weights, int[] values, int W) { int n = weights.Length; int[,] dp = new int[n + 1, W + 1]; for (int i = 1; i &lt;= n; i++) { for (int w = 1; w &lt;= W; w++) { if (weights[i - 1] &lt;= w)…

Coding Read answer
Mid PDF
Reverse a string (without built-in reverse)?

Short answer: Logic Convert to char array. Swap characters from both ends. string str = &quot;dotnet&quot;; char[] chars = str.ToCharArray(); int left = 0, right = chars.Length - 1; while (left &lt; right) { char temp =…

Coding Scenarios Read answer
Mid PDF
Calculate power of a number without built-in pow()?

Short answer: public double Power(double x, int n) { if (n == 0) return 1; double half = Power(x, n / 2); if (n % 2 == 0) return half * half; else return n &gt; 0 ? x * half * half : (half * half) / x; } Explanation: Use…

Coding Read answer
Mid PDF
Regular Expression Matching (. and *) bool IsMatch(string s, string p) { return IsMatchHelper(s, p, 0, 0); } bool IsMatchHelper(string s, string p, int i, int j) { if (j == p.Length) return i == s.Length; bool firstMatch = (i < s.Length) && (p[j] == s[i] || p[j] == '.'); if (j + 1 < p.Length && p[j + 1] == '*') { // Two cases: // 1) Use zero occurrence of p[j] (skip) // 2) If firstMatch, consume one char in s and keep pattern

Short answer: t j return IsMatchHelper(s, p, i, j + 2) || (firstMatch &amp;&amp; IsMatchHelper(s, p, i + 1, j)); } else { return firstMatch &amp;&amp; IsMatchHelper(s, p, i + 1, j + 1); } Follow on: } Explanation: Recurs…

Coding Read answer
Mid PDF
Check if two numbers have opposite signs?

Short answer: public bool HaveOppositeSigns(int x, int y) { return (x ^ y) &lt; 0; } Explanation: XOR of two numbers with opposite signs has the sign bit set (negative number). Example code public bool HaveOppositeSigns(…

Coding Read answer

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } TreeNode prev = null; TreeNode head = null; TreeNode ConvertToDLL(TreeNode root) { if (root == null) return null; ConvertToDLL(root.left); if (prev == null) { head = root; // first node becomes head } else { root.left = prev; Follow on: prev.right = root; } prev = root; ConvertToDLL(root.right); return head; }…

Explain a bit more

Explanation: Inorder traversal connects nodes as doubly linked list by linking current with previous node.

Example code

public class TreeNode {
public int val;
public TreeNode left, right;
public TreeNode(int x) { val = x; }
}
TreeNode prev = null;
TreeNode head = null; TreeNode ConvertToDLL(TreeNode root) { if (root == null) return null; ConvertToDLL(root.left); if (prev == null) {
head = root; // first node becomes head } else { root.left = prev; Follow on: prev.right = root;
}
prev = root; ConvertToDLL(root.right); return head;
} Explanation: Inorder traversal connects nodes as doubly linked list by linking current with previous node.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public class MyQueue { private Stack<int> stackIn = new Stack<int>(); private Stack<int> stackOut = new Stack<int>(); // Enqueue: push into stackIn (O(1)) public void Enqueue(int x) { stackIn.Push(x); } // Dequeue: if stackOut empty, pour all from stackIn to stackOut, then pop (amortized O(1)) public int Dequeue() { if (stackOut.Count == 0) { while (stackIn.Count > 0) { stackOut.Push(stackIn.Pop()); } } return…

Explain a bit more

stackOut.Pop(); } public int Peek() { if (stackOut.Count == 0) { while (stackIn.Count > 0) { stackOut.Push(stackIn.Pop()); } } return stackOut.Peek(); } public bool IsEmpty() { return stackIn.Count == 0 && stackOut.Count == 0; } } Follow on: Explanation: Two stacks are used: stackIn for enqueue, stackOut for dequeue. Elements are transferred only when needed, making both operations amortized O(1).

Example code

public class MyQueue {
private Stack<int> stackIn = new Stack<int>();
private Stack<int> stackOut = new Stack<int>(); // Enqueue: push into stackIn (O(1)) public void Enqueue(int x) { stackIn.Push(x); } // Dequeue: if stackOut empty, pour all from stackIn to stackOut, then pop (amortized O(1)) public int Dequeue() {
if (stackOut.Count == 0) { while (stackIn.Count > 0) { stackOut.Push(stackIn.Pop()); }
}
return stackOut.Pop();
}
public int Peek() {
if (stackOut.Count == 0) { while (stackIn.Count > 0) { stackOut.Push(stackIn.Pop()); }
}
return stackOut.Peek();
}
public bool IsEmpty() {
return stackIn.Count == 0 && stackOut.Count == 0;
}
} Follow on: Explanation: Two stacks are used: stackIn for enqueue, stackOut for dequeue. Elements are transferred only when needed, making both operations amortized O(1).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: begins public class ListNode {

Example code

public int val;
public ListNode next;
public ListNode(int x) { val = x; next = null; }
}
ListNode DetectCycle(ListNode head) {
if (head == null) return null;
ListNode slow = head, fast = head; Follow on: // Detect cycle using Floyd's Tortoise and Hare while (fast != null && fast.next != null) { slow = slow.next;
fast = fast.next.next;
if (slow == fast) { // cycle detected
ListNode ptr = head; while (ptr != slow) { ptr = ptr.next;
slow = slow.next;
}
return ptr; // start node of cycle
}
}
return null; // no cycle
} Explanation: First detect cycle meeting point with two pointers. Then find start node by moving one pointer from head and one from meeting point until they meet.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int MergeSortAndCount(int[] arr, int[] temp, int left, int right) { int invCount = 0; if (right > left) { int mid = (right + left) / 2; invCount += MergeSortAndCount(arr, temp, left, mid); invCount += MergeSortAndCount(arr, temp, mid + 1, right); invCount += Merge(arr, temp, left, mid + 1, right); } return invCount; } int Merge(int[] arr, int[] temp, int left, int mid, int right) { int i = left, j = mid, k = left;…

Explain a bit more

int invCount = 0; while (i <= mid - 1 && j <= right) { if (arr[i] <= arr[j]) temp[k++] = arr[i++]; else { temp[k++] = arr[j++]; invCount += (mid - i); // Count inversions } } while (i <= mid - 1) temp[k++] = arr[i++]; Follow on: while (j <= right) temp[k++] = arr[j++]; for (int idx = left; idx <= right; idx++) arr[idx] = temp[idx]; return invCount; } Explanation: Using a modified merge sort to count pairs where arr[i] > arr[j] for i < j efficiently.

Example code

int MergeSortAndCount(int[] arr, int[] temp, int left, int right)
{
int invCount = 0;
if (right > left)
{
int mid = (right + left) / 2;
invCount += MergeSortAndCount(arr, temp, left, mid);
invCount += MergeSortAndCount(arr, temp, mid + 1, right);
invCount += Merge(arr, temp, left, mid + 1, right);
}
return invCount;
}
int Merge(int[] arr, int[] temp, int left, int mid, int right)
{
int i = left, j = mid, k = left;
int invCount = 0; while (i <= mid - 1 && j <= right) {
if (arr[i] <= arr[j])
temp[k++] = arr[i++]; else {
temp[k++] = arr[j++];
invCount += (mid - i); // Count inversions
}
} while (i <= mid - 1) temp[k++] = arr[i++]; Follow on: while (j <= right) temp[k++] = arr[j++];
for (int idx = left; idx <= right; idx++)
arr[idx] = temp[idx];
return invCount;
} Explanation: Using a modified merge sort to count pairs where arr[i] > arr[j] for i < j efficiently.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: Algorithm int GCD(int a, int b) { Follow on: while (b != 0) {

Example code

int temp = b;
b = a % b;
a = temp;
}
return a;
} Explanation: Repeatedly replace (a, b) with (b, a mod b) until b is 0; then a is the GCD.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: void QuickSort(int[] arr, int low, int high) {

Example code

if (low < high) Follow on: {
int pi = Partition(arr, low, high); QuickSort(arr, low, pi - 1); QuickSort(arr, pi + 1, high); }
}
int Partition(int[] arr, int low, int high)
{
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] < pivot)
{ i++; (arr[i], arr[j]) = (arr[j], arr[i]);
}
}
(arr[i + 1], arr[high]) = (arr[high], arr[i + 1]);
return i + 1;
} Explanation: Choose last element as pivot, partition array so left < pivot < right, recursively sort subarrays.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int Fibonacci(int n) { if (n <= 1) return n; int a = 0, b = 1; for (int i = 2; i <= n; i++) { int temp = a + b; a = b; b = temp; } return b; } Explanation: Iterative DP approach; each Fibonacci number is sum of two previous. Follow on:

Example code

int Fibonacci(int n)
{
if (n <= 1) return n;
int a = 0, b = 1;
for (int i = 2; i <= n; i++)
{
int temp = a + b;
a = b;
b = temp;
}
return b;
} Explanation: Iterative DP approach; each Fibonacci number is sum of two previous. Follow on:

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: lternative using Brian Kernighan’s algorithm: public int CountSetBits(int n) { int count = 0; while (n != 0) { n &amp;= (n - 1); // Drops the lowest set bit count++; } return count; } Follow on: lternative using Brian Kernighan’s algorithm: public int CountSetBits(int n) { int count = 0; while (n != 0) { n &amp;= (n - 1); // Drops… the lowest set bit…… lternative using Brian Kernighan’s algorithm: public int…

Explain a bit more

CountSetBits(int n) { int count = 0; while (n != 0) { n &amp;= (n - 1); // Drops the lowest set bit count++; } return count; } Follow on: lternative using Brian Kernighan’s algorithm: public int CountSetBits(int n) { int count = 0; while (n != 0) { n &amp;= (n - 1); // Drops… the lowest set bit count++; } return count; } Follow on:

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: rray.Copy(arr, left, L, 0, n1); rray.Copy(arr, mid + 1, R, 0, n2); int i = 0, j = 0, k = left; while (i < n1 && j < n2) rr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++]; while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } Explanation: Divide array, sort left & right halves, then merge sorted halves. while (i < n1) arr[k++] = L[i++];

Example code

while (j < n2) arr[k++] = R[j++];
} Explanation: Divide array, sort left & right halves, then merge sorted halves.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public 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 efficiently removes the lowest set bit each iteration until zero.

Example code

public 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 efficiently removes the lowest set bit each iteration until zero.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public string LongestCommonPrefix(string[] strs) { if (strs == null || strs.Length == 0) return ""; for (int i = 0; i < strs[0].Length; i++) { char c = strs[0][i]; for (int j = 1; j < strs.Length; j++) { if (i == strs[j].Length || strs[j][i] != c) return strs[0].Substring(0, i); } } return strs[0]; } Follow on: Explanation: Check character by character across all strings until mismatch.

Example code

public string LongestCommonPrefix(string[] strs) {
if (strs == null || strs.Length == 0) return "";
for (int i = 0; i < strs[0].Length; i++) {
char c = strs[0][i];
for (int j = 1; j < strs.Length; j++) {
if (i == strs[j].Length || strs[j][i] != c)
return strs[0].Substring(0, i);
}
}
return strs[0];
} Follow on: Explanation: Check character by character across all strings until mismatch.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public int[] SortNearlySorted(int[] nums, int k) { var result = new List<int>(); var minHeap = new SortedSet<(int val, int index)>(); for (int i = 0; i < nums.Length; i++) { minHeap.Add((nums[i], i)); if (minHeap.Count > k) { var min = minHeap.Min; minHeap.Remove(min); result.Add(min.val); } } while (minHeap.Count > 0) { var min = minHeap.Min; minHeap.Remove(min); result.Add(min.val); } return result.ToArray(); }…

Explain a bit more

Explanation: Use a min-heap of size k+1 to always extract the smallest element in the current window.

Example code

public int[] SortNearlySorted(int[] nums, int k) {
var result = new List<int>();
var minHeap = new SortedSet<(int val, int index)>();
for (int i = 0; i < nums.Length; i++) { minHeap.Add((nums[i], i)); if (minHeap.Count > k) {
var min = minHeap.Min; minHeap.Remove(min); result.Add(min.val); }
} while (minHeap.Count > 0) { var min = minHeap.Min; minHeap.Remove(min); result.Add(min.val); }
return result.ToArray();
} Explanation: Use a min-heap of size k+1 to always extract the smallest element in the current window.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int ClimbStairs(int n) { if (n <= 2) return n; int a = 1, b = 2; for (int i = 3; i <= n; i++) { int c = a + b; a = b; b = c; } return b; }

Example code

int ClimbStairs(int n) {
if (n <= 2) return n;
int a = 1, b = 2;
for (int i = 3; i <= n; i++) {
int c = a + b;
a = b;
b = c;
}
return b;
}

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int CountConnectedComponents(Dictionary<int, List<int>> graph) { var visited = new HashSet<int>(); int count = 0; Follow on: foreach (var node in graph.Keys) { if (!visited.Contains(node)) { DFS(node, graph, visited); count++; } } return count; } void DFS(int node, Dictionary<int, List<int>> graph, HashSet<int> visited) { visited.Add(node); foreach (var neighbor in graph[node]) { if (!visited.Contains(neighbor)) {…

Explain a bit more

DFS(neighbor, graph, visited); } } } Explanation: Run DFS on unvisited nodes, count how many times DFS starts.

Example code

int CountConnectedComponents(Dictionary<int, List<int>> graph) {
var visited = new HashSet<int>();
int count = 0; Follow on: foreach (var node in graph.Keys) {
if (!visited.Contains(node)) { DFS(node, graph, visited); count++; }
}
return count;
} void DFS(int node, Dictionary<int, List<int>> graph, HashSet<int> visited) { visited.Add(node); foreach (var neighbor in graph[node]) {
if (!visited.Contains(neighbor)) { DFS(neighbor, graph, visited); }
}
} Explanation: Run DFS on unvisited nodes, count how many times DFS starts.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: void VerticalSum(TreeNode root, int hd, Dictionary<int, int> map) { if (root == null) return; VerticalSum(root.left, hd - 1, map); if (map.ContainsKey(hd)) map[hd] += root.val; else map[hd] = root.val; VerticalSum(root.right, hd + 1, map); } Dictionary<int, int> GetVerticalSum(TreeNode root) { var map = new Dictionary<int, int>(); VerticalSum(root, 0, map); return map; } Explanation: Use horizontal distance (hd)…

Explain a bit more

from root; sum values of nodes at each hd. Follow on:

Example code

void VerticalSum(TreeNode root, int hd, Dictionary<int, int> map) { if (root == null) return; VerticalSum(root.left, hd - 1, map); if (map.ContainsKey(hd))
map[hd] += root.val; else map[hd] = root.val; VerticalSum(root.right, hd + 1, map); }
Dictionary<int, int> GetVerticalSum(TreeNode root) {
var map = new Dictionary<int, int>(); VerticalSum(root, 0, map); return map;
} Explanation: Use horizontal distance (hd) from root; sum values of nodes at each hd. Follow on:

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public int EvaluateInfix(string expression) { Stack<int> operands = new Stack<int>(); Stack<char> operators = new Stack<char>(); int i = 0; while (i < expression.Length) { if (char.IsWhiteSpace(expression[i])) { i++; continue; } if (char.IsDigit(expression[i])) { int val = 0; while (i < expression.Length && char.IsDigit(expression[i])) { val = val * 10 + (expression[i] - '0'); i++; } operands.Push(val); continue; }…

Explain a bit more

if (expression[i] == '(') { operators.Push(expression[i]); } else if (expression[i] == ')') { while (operators.Peek() != '(') { ApplyOp(operands, operators); } operators.Pop(); // remove '(' Follow on: } else if (IsOperator(expression[i])) { while (operators.Count > 0 && Precedence(operators.Peek()) >= Precedence(expression[i])) { ApplyOp(operands, operators); } operators.Push(expression[i]); } i++; } while (operators.Count > 0) { ApplyOp(operands, operators); } return operands.Pop(); } bool IsOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } int Precedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; } void ApplyOp(Stack<int> operands, Stack<char> operators) { int b = operands.Pop(); int a = operands.Pop(); char op = operators.Pop(); int result = 0; switch (op) { case '+': result = a + b; break; case '-':…

Example code

public int EvaluateInfix(string expression) {
Stack<int> operands = new Stack<int>();
Stack<char> operators = new Stack<char>();
int i = 0; while (i < expression.Length) { if (char.IsWhiteSpace(expression[i])) { i++; continue; }
if (char.IsDigit(expression[i])) {
int val = 0; while (i < expression.Length && char.IsDigit(expression[i])) { val = val * 10 + (expression[i] - '0'); i++; } operands.Push(val); continue; }
if (expression[i] == '(') { operators.Push(expression[i]); } else if (expression[i] == ')') { while (operators.Peek() != '(') { ApplyOp(operands, operators); } operators.Pop(); // remove '(' Follow on: } else if (IsOperator(expression[i])) { while (operators.Count > 0 && Precedence(operators.Peek()) >= Precedence(expression[i])) { ApplyOp(operands, operators); } operators.Push(expression[i]); } i++; } while (operators.Count > 0) { ApplyOp(operands, operators); }
return operands.Pop();
} bool IsOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/';
}
int Precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
} void ApplyOp(Stack<int> operands, Stack<char> operators) { int b = operands.Pop();
int a = operands.Pop();
char op = operators.Pop();
int result = 0; switch (op) { case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break; Follow on: case '/': result = a / b; break;
} operands.Push(result); } Explanation: Standard two-stack algorithm for evaluating infix expressions considering operator precedence and parentheses.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: ListNode ReverseBetween(ListNode head, int m, int n) {

Example code

if (head == null || m == n) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy; // Move prev to one before m-th node for (int i = 1; i < m; i++) prev = prev.next;
ListNode start = prev.next;
ListNode then = start.next; // Reverse the sublist for (int i = 0; i < n - m; i++) { Follow on: start.next = then.next;
then.next = prev.next;
prev.next = then;
then = start.next;
}
return dummy.next;
} Explanation: Use a dummy node to simplify edge cases. Reverse nodes between m and n by changing pointers.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: string LongestPalindrome(string s) { if (string.IsNullOrEmpty(s)) return ""; int start = 0, maxLen = 1; for (int i = 0; i < s.Length; i++) { ExpandAroundCenter(s, i, i, ref start, ref maxLen); // Odd length palindrome ExpandAroundCenter(s, i, i + 1, ref start, ref maxLen); // Even length palindrome } return s.Substring(start, maxLen); } void ExpandAroundCenter(string s, int left, int right, ref int start, ref int…

Explain a bit more

maxLen) { while (left >= 0 && right < s.Length && s[left] == s[right]) { if (right - left + 1 > maxLen) Follow on: { start = left; maxLen = right - left + 1; } left--; right++; } } Explanation: Expand around each center to find the longest palindrome in O(n²).

Example code

string LongestPalindrome(string s)
{
if (string.IsNullOrEmpty(s)) return "";
int start = 0, maxLen = 1;
for (int i = 0; i < s.Length; i++)
{ ExpandAroundCenter(s, i, i, ref start, ref maxLen); // Odd length palindrome ExpandAroundCenter(s, i, i + 1, ref start, ref maxLen); // Even length palindrome }
return s.Substring(start, maxLen);
} void ExpandAroundCenter(string s, int left, int right, ref int start, ref int maxLen) { while (left >= 0 && right < s.Length && s[left] == s[right]) {
if (right - left + 1 > maxLen) Follow on: {
start = left;
maxLen = right - left + 1;
} left--; right++; }
} Explanation: Expand around each center to find the longest palindrome in O(n²).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int LCM(int a, int b) { return a / GCD(a, b) * b; } Explanation: LCM × GCD = product of the two numbers.

Example code

int LCM(int a, int b)
{
return a / GCD(a, b) * b;
} Explanation: LCM × GCD = product of the two numbers.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: void MergeSort(int[] arr, int left, int right) {

Example code

if (left < right)
{
int mid = (left + right) / 2; MergeSort(arr, left, mid); MergeSort(arr, mid + 1, right); Follow on: Merge(arr, left, mid, right); }
} void Merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2]; Array.Copy(arr, left, L, 0, n1); Array.Copy(arr, mid + 1, R, 0, n2); int i = 0, j = 0, k = left; while (i < n1 && j < n2) arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
} Explanation: Divide array, sort left & right halves, then merge sorted halves.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: int Knapsack(int[] weights, int[] values, int W) { int n = weights.Length; int[,] dp = new int[n + 1, W + 1]; for (int i = 1; i <= n; i++) { for (int w = 1; w <= W; w++) { if (weights[i - 1] <= w) dp[i, w] = Math.Max(dp[i - 1, w], values[i - 1] + dp[i - 1, w - weights[i - 1]]); else dp[i, w] = dp[i - 1, w]; } } return dp[n, W]; } Explanation: Build table where dp[i,w] = max value using first i items and capacity w.

Example code

int Knapsack(int[] weights, int[] values, int W)
{
int n = weights.Length;
int[,] dp = new int[n + 1, W + 1];
for (int i = 1; i <= n; i++)
{
for (int w = 1; w <= W; w++)
{
if (weights[i - 1] <= w) dp[i, w] = Math.Max(dp[i - 1, w], values[i - 1] + dp[i - 1, w - weights[i - 1]]); else dp[i, w] = dp[i - 1, w];
}
}
return dp[n, W];
} Explanation: Build table where dp[i,w] = max value using first i items and capacity w. Follow on:

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding Scenarios

Short answer: Logic Convert to char array. Swap characters from both ends. string str = "dotnet"; char[] chars = str.ToCharArray(); int left = 0, right = chars.Length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } string reversed = new string(chars);

Example code

Logic Convert to char array. Swap characters from both ends. string str = "dotnet";
char[] chars = str.ToCharArray();
int left = 0, right = chars.Length - 1; while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp; left++; right--; }
string reversed = new string(chars);

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public double Power(double x, int n) { if (n == 0) return 1; double half = Power(x, n / 2); if (n % 2 == 0) return half * half; else return n > 0 ? x * half * half : (half * half) / x; } Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n).

Example code

public double Power(double x, int n) {
if (n == 0) return 1;
double half = Power(x, n / 2);
if (n % 2 == 0)
return half * half; else return n > 0 ? x * half * half : (half * half) / x;
} Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: t j return IsMatchHelper(s, p, i, j + 2) || (firstMatch && IsMatchHelper(s, p, i + 1, j)); } else { return firstMatch && IsMatchHelper(s, p, i + 1, j + 1); } Follow on: } Explanation: Recursively matches strings supporting '.' (any char) and '*' (zero or more of preceding).

Explain a bit more

t j return IsMatchHelper(s, p, i, j + 2) || (firstMatch && IsMatchHelper(s, p, i + 1, j)); } else { return firstMatch && IsMatchHelper(s, p, i + 1, j + 1); } Follow on: } Explanation: Recursively matches strings supporting '.' (any char) and '*' (zero or more of preceding). t j return IsMatchHelper(s, p, i, j + 2) || (firstMatch && IsMatchHelper(s, p, i + 1, j)); } else { return firstMatch && IsMatchHelper(s, p, i + 1, j + 1); } Follow on: } Explanation: Recursively matches strings supporting '.' (any char) and '*' (zero or more of preceding).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Short answer: public bool HaveOppositeSigns(int x, int y) { return (x ^ y) < 0; } Explanation: XOR of two numbers with opposite signs has the sign bit set (negative number).

Example code

public bool HaveOppositeSigns(int x, int y) {
return (x ^ y) < 0;
} Explanation: XOR of two numbers with opposite signs has the sign bit set (negative number).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Permalink & share
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details