Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: public class NestedIterator { private Queue<int> queue; public NestedIterator(IList<NestedInteger> nestedList) { queue = new Queue<int>(); Flatten(nestedList); } private void Flatten(IList…
Short answer: x * half * half : (half * half) / x; Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n). Explain a bit more x * half * half : (half * half) / x; Explanation: Uses fast e…
Short answer: public class MedianFinder { private PriorityQueue<int, int> maxHeap; // lower half (max heap) Follow on: private PriorityQueue<int, int> minHeap; // upper half (min heap) public MedianFinder() {…
Short answer: int HouseRobber(int[] nums) { if (nums.Length == 0) return 0; if (nums.Length == 1) return nums[0]; int prev1 = 0, prev2 = 0; foreach (var num in nums) { int temp = prev1; prev1 = Math.Max(prev2 + num, prev…
Short answer: public class Edge { public int Source, Dest, Weight; public Edge(int s, int d, int w) { Source = s; Dest = d; Weight = w; } } int[] BellmanFord(int vertices, List<Edge> edges, int source) { int[] dist…
Short answer: TreeNode prev = null; void Flatten(TreeNode root) { if (root == null) return; Flatten(root.right); Flatten(root.left); root.right = prev; root.left = null; prev = root; } Explanation: Postorder traversal (r…
Short answer: int[] NextGreaterElements(int[] nums) { int n = nums.Length; int[] result = new int[n]; Stack<int> stack = new Stack<int>(); for (int i = n - 1; i >= 0; i--) { while (stack.Count > 0 &…
Short answer: ListNode MergeKLists(ListNode[] lists) { Example code if (lists == null || lists.Length == 0) return null; PriorityQueue<ListNode, int> pq = new PriorityQueue<ListNode, int>(); foreach (var list…
Short answer: 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) &am…
Short answer: List<List<int>> Subsets(int[] nums) Example code { List<List<int>> result = new List<List<int>>(); GenerateSubsets(nums, 0, new List<int>(), result); return result;…
Short answer: bool IsPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return…
Short answer: int FirstOccurrence(int[] arr, int target) { int low = 0, high = arr.Length - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; Follow on: hi…
Short answer: int LCS(string s1, string s2) { int m = s1.Length, n = s2.Length; int[,] dp = new int[m + 1, n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) dp[i, j]…
Short answer: ListNode GetIntersectionNode(ListNode headA, ListNode headB) { Example code if (headA == null || headB == null) return null; ListNode a = headA, b = headB; while (a != b) { a = (a == null) ? headB : a.next;…
Short answer: Logic Same length Same character frequency bool IsAnagram(string s1, string s2) { Example code if (s1.Length != s2.Length) return false; int[] count = new int[256]; foreach (char c in s1) count[c]++; foreac…
Short answer: = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; // either intersection or null } Explanation: Two pointers traverse both lists; if no intersection, both will reach null simulta…
Short answer: rray.Fill(dp, 1); int maxLen = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + 1); } maxLen = Math.Max(maxLen, dp[i]); } return…
Short answer: public int FindMissingNumber(int[] nums, int n) { int xor = 0; for (int i = 1; i <= n; i++) { xor ^= i; } foreach (int num in nums) { xor ^= num; } return xor; } Follow on: Explanation: XOR all numbers f…
Short answer: public int MaximumTotal(IList<IList<int>> triangle) { int n = triangle.Count; int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = triangle[n - 1][i]; for (int layer = n - 2; layer >…
Short answer: public bool IsPerfectSquare(int num) { if (num < 0) return false; int left = 0, right = num; while (left <= right) { int mid = left + (right - left) / 2; long sq = (long)mid * mid; if (sq == num) retu…
Short answer: public int CountPartitions(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return 0; int target = sum / 2; int[] dp = new int[target + 1]; dp[0] = 1; Follow on: foreach (i…
Short answer: int LongestPalindromeSubseq(string s) { int n = s.Length; int[,] dp = new int[n, n]; for (int i = n - 1; i >= 0; i--) { dp[i, i] = 1; for (int j = i + 1; j < n; j++) { if (s[i] == s[j]) Follow on: dp[…
Short answer: List<List<int>> TarjanSCC(Dictionary<int, List<int>> graph) { Example code int time = 0; var stack = new Stack<int>(); var onStack = new HashSet<int>(); var low = new Dic…
Short answer: bool IsIdentical(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null) return false; if (p.val != q.val) return false; return IsIdentical(p.left, q.left) &am…
Short answer: element in constant time public class MinStack { Example code private Stack<int> stack = new Stack<int>(); private Stack<int> minStack = new Stack<int>(); Follow on: public void Push…
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public class NestedIterator { private Queue<int> queue; public NestedIterator(IList<NestedInteger> nestedList) { queue = new Queue<int>(); Flatten(nestedList); } private void Flatten(IList<NestedInteger> nestedList) { foreach (var ni in nestedList) { if (ni.IsInteger()) queue.Enqueue(ni.GetInteger()); else Flatten(ni.GetList()); } } public bool HasNext() { return queue.Count > 0; } public int Next() { return…
queue.Dequeue(); } } Note: NestedInteger is an interface with methods: IsInteger(), GetInteger(), GetList(). Explanation: Pre-flatten the nested list into a queue and iterate over it. Follow on:
public class NestedIterator {
private Queue<int> queue;
public NestedIterator(IList<NestedInteger> nestedList) {
queue = new Queue<int>(); Flatten(nestedList); }
private void Flatten(IList<NestedInteger> nestedList) {
foreach (var ni in nestedList) {
if (ni.IsInteger()) queue.Enqueue(ni.GetInteger()); else Flatten(ni.GetList()); }
}
public bool HasNext() {
return queue.Count > 0;
}
public int Next() {
return queue.Dequeue();
}
} Note: NestedInteger is an interface with methods: IsInteger(), GetInteger(), GetList(). Explanation: Pre-flatten the nested list into a queue and iterate over it. Follow on:
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: x * half * half : (half * half) / x; Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n).
x * half * half : (half * half) / x; Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n). x * half * half : (half * half) / x; Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n). x * half * half : (half * half) / x; Explanation: Uses fast exponentiation (divide and conquer) to calculate x^n in O(log n).
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public class MedianFinder { private PriorityQueue<int, int> maxHeap; // lower half (max heap) Follow on: private PriorityQueue<int, int> minHeap; // upper half (min heap) public MedianFinder() { maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a))); minHeap = new PriorityQueue<int, int>(); } public void AddNum(int num) { maxHeap.Enqueue(num, num); minHeap.Enqueue(maxHeap.Dequeue(),…
maxHeap.Peek()); if (maxHeap.Count < minHeap.Count) maxHeap.Enqueue(minHeap.Dequeue(), minHeap.Peek()); } public double FindMedian() { if (maxHeap.Count > minHeap.Count) return maxHeap.Peek(); return (maxHeap.Peek() + minHeap.Peek()) / 2.0; } } Explanation: Maintain two heaps: maxHeap for lower half, minHeap for upper half. Balance their sizes.
public class MedianFinder {
private PriorityQueue<int, int> maxHeap; // lower half (max heap) Follow on: private PriorityQueue<int, int> minHeap; // upper half (min heap) public MedianFinder() { maxHeap = new PriorityQueue<int, int>(Comparer<int>.Create((a, b) => b.CompareTo(a)));
minHeap = new PriorityQueue<int, int>();
}
public void AddNum(int num) { maxHeap.Enqueue(num, num); minHeap.Enqueue(maxHeap.Dequeue(), maxHeap.Peek()); if (maxHeap.Count < minHeap.Count) maxHeap.Enqueue(minHeap.Dequeue(), minHeap.Peek()); }
public double FindMedian() {
if (maxHeap.Count > minHeap.Count)
return maxHeap.Peek();
return (maxHeap.Peek() + minHeap.Peek()) / 2.0;
}
} Explanation: Maintain two heaps: maxHeap for lower half, minHeap for upper half. Balance their sizes.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: int HouseRobber(int[] nums) { if (nums.Length == 0) return 0; if (nums.Length == 1) return nums[0]; int prev1 = 0, prev2 = 0; foreach (var num in nums) { int temp = prev1; prev1 = Math.Max(prev2 + num, prev1); prev2 = temp; } return prev1; }
int HouseRobber(int[] nums) {
if (nums.Length == 0) return 0;
if (nums.Length == 1) return nums[0];
int prev1 = 0, prev2 = 0;
foreach (var num in nums) {
int temp = prev1;
prev1 = Math.Max(prev2 + num, prev1);
prev2 = temp;
}
return prev1;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public class Edge { public int Source, Dest, Weight; public Edge(int s, int d, int w) { Source = s; Dest = d; Weight = w; } } int[] BellmanFord(int vertices, List<Edge> edges, int source) { int[] dist = new int[vertices]; for (int i = 0; i < vertices; i++) dist[i] = int.MaxValue; dist[source] = 0; Follow on: for (int i = 1; i < vertices; i++) { foreach (var edge in edges) { if (dist[edge.Source] != int.MaxValue &&…
dist[edge.Source] + edge.Weight < dist[edge.Dest]) { dist[edge.Dest] = dist[edge.Source] + edge.Weight; } } } // Detect negative weight cycle (optional) foreach (var edge in edges) { if (dist[edge.Source] != int.MaxValue && dist[edge.Source] + edge.Weight < dist[edge.Dest]) { throw new Exception("Graph contains negative weight cycle"); } } return dist; } Explanation: Relax edges V-1 times, then check for negative weight cycles.
public class Edge {
public int Source, Dest, Weight;
public Edge(int s, int d, int w) {
Source = s; Dest = d; Weight = w;
}
}
int[] BellmanFord(int vertices, List<Edge> edges, int source) {
int[] dist = new int[vertices];
for (int i = 0; i < vertices; i++) dist[i] = int.MaxValue;
dist[source] = 0; Follow on: for (int i = 1; i < vertices; i++) {
foreach (var edge in edges) {
if (dist[edge.Source] != int.MaxValue && dist[edge.Source] + edge.Weight < dist[edge.Dest]) { dist[edge.Dest] = dist[edge.Source] + edge.Weight;
}
}
} // Detect negative weight cycle (optional) foreach (var edge in edges) {
if (dist[edge.Source] != int.MaxValue && dist[edge.Source] + edge.Weight < dist[edge.Dest]) { throw new Exception("Graph contains negative weight cycle"); }
}
return dist;
} Explanation: Relax edges V-1 times, then check for negative weight cycles.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: TreeNode prev = null; void Flatten(TreeNode root) { if (root == null) return; Flatten(root.right); Flatten(root.left); root.right = prev; root.left = null; prev = root; } Explanation: Postorder traversal (right-left-root) to flatten tree in place.
TreeNode prev = null; void Flatten(TreeNode root) { if (root == null) return; Flatten(root.right); Flatten(root.left); root.right = prev;
root.left = null;
prev = root;
} Explanation: Postorder traversal (right-left-root) to flatten tree in place.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: int[] NextGreaterElements(int[] nums) { int n = nums.Length; int[] result = new int[n]; Stack<int> stack = new Stack<int>(); for (int i = n - 1; i >= 0; i--) { while (stack.Count > 0 && stack.Peek() <= nums[i]) { stack.Pop(); } result[i] = stack.Count == 0 ? -1 : stack.Peek(); stack.Push(nums[i]); } return result; } Explanation: Traverse from right to left, use stack to keep track of next greater elements in O(n).
int[] NextGreaterElements(int[] nums) {
int n = nums.Length;
int[] result = new int[n];
Stack<int> stack = new Stack<int>();
for (int i = n - 1; i >= 0; i--) { while (stack.Count > 0 && stack.Peek() <= nums[i]) { stack.Pop(); }
result[i] = stack.Count == 0 ? -1 : stack.Peek(); stack.Push(nums[i]); }
return result;
} Explanation: Traverse from right to left, use stack to keep track of next greater elements in O(n).
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: ListNode MergeKLists(ListNode[] lists) {
if (lists == null || lists.Length == 0) return null; PriorityQueue<ListNode, int> pq = new PriorityQueue<ListNode, int>();
foreach (var list in lists)
if (list != null) pq.Enqueue(list, list.val); ListNode dummy = new ListNode(0);
ListNode current = dummy; while (pq.Count > 0) { var node = pq.Dequeue();
current.next = node;
current = current.next;
if (node.next != null) pq.Enqueue(node.next, node.next.val); }
return dummy.next;
} Follow on: Explanation: Use a min-heap (priority queue) to always pick the smallest head node among k lists.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: 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 at 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).
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 at 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).
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: List<List<int>> Subsets(int[] nums)
{
List<List<int>> result = new List<List<int>>(); GenerateSubsets(nums, 0, new List<int>(), result); return result;
} void GenerateSubsets(int[] nums, int index, List<int> current, List<List<int>> result)
{
if (index == nums.Length)
{ result.Add(new List<int>(current)); return;
} // Exclude nums[index] GenerateSubsets(nums, index + 1, current, result); // Include nums[index] current.Add(nums[index]); GenerateSubsets(nums, index + 1, current, result); current.RemoveAt(current.Count - 1); Follow on: } Explanation: Backtracking approach includes/excludes each element.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: bool IsPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) return false; Follow on: } return true; } Explanation: Check divisibility by 2, 3, then test possible divisors of form 6k ± 1 up to √n.
bool IsPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false; Follow on: }
return true;
} Explanation: Check divisibility by 2, 3, then test possible divisors of form 6k ± 1 up to √n.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: int FirstOccurrence(int[] arr, int target) { int low = 0, high = arr.Length - 1, result = -1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { result = mid; Follow on: high = mid - 1; // search left side } else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return result; } Explanation: Binary search but continue left to find first occurrence.
int FirstOccurrence(int[] arr, int target)
{
int low = 0, high = arr.Length - 1, result = -1; while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target)
{
result = mid; Follow on: high = mid - 1; // search left side
} else if (arr[mid] < target) low = mid + 1; else high = mid - 1;
}
return result;
} Explanation: Binary search but continue left to find first occurrence.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: int LCS(string s1, string s2) { int m = s1.Length, n = s2.Length; int[,] dp = new int[m + 1, n + 1]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) dp[i, j] = dp[i - 1, j - 1] + 1; else dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]); } } return dp[m, n]; } Explanation: Build DP table comparing chars; find longest subsequence common to both strings. Follow on:
int LCS(string s1, string s2)
{
int m = s1.Length, n = s2.Length;
int[,] dp = new int[m + 1, n + 1];
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (s1[i - 1] == s2[j - 1])
dp[i, j] = dp[i - 1, j - 1] + 1; else dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]);
}
}
return dp[m, n];
} Explanation: Build DP table comparing chars; find longest subsequence common to both strings. Follow on:
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode a = headA, b = headB; while (a != b) { a = (a == null) ? headB : a.next;
b = (b == null) ? headA : b.next;
}
return a; // either intersection or null
} Explanation: Two pointers traverse both lists; if no intersection, both will reach null simultaneously.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: Logic Same length Same character frequency bool IsAnagram(string s1, string s2) {
if (s1.Length != s2.Length) return false;
int[] count = new int[256];
foreach (char c in s1) count[c]++;
foreach (char c in s2) count[c]--;
foreach (int i in count)
if (i != 0) return false;
return true;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; // either intersection or null } Explanation: Two pointers traverse both lists; if no intersection, both will reach null simultaneously. = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; // either intersection or null }… Explanation: Two pointers…… traverse both lists; if no intersection, both will reach null…
simultaneously. = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; // either intersection or null } Explanation: Two pointers traverse both lists; if no intersection, both will reach null simultaneously. = (a == null) ? headB : a.next; b = (b == null) ? headA : b.next; } return a; // either intersection or null }… Explanation: Two pointers traverse both lists; if no intersection, both will reach null simultaneously.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: rray.Fill(dp, 1); int maxLen = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + 1); } maxLen = Math.Max(maxLen, dp[i]); } return maxLen; } Explanation: For each element, find LIS ending there by checking previous smaller elements.
Follow on: rray.Fill(dp, 1); int maxLen = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + 1); } maxLen = Math.Max(maxLen, dp[i]); } return maxLen; } Explanation: For each element, find LIS ending there by checking previous smaller elements. for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (nums[i] > nums[j])
dp[i] = Math.Max(dp[i], dp[j] + 1);
}
maxLen = Math.Max(maxLen, dp[i]);
}
return maxLen;
} Explanation: For each element, find LIS ending there by checking previous smaller elements. Follow on:
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public int FindMissingNumber(int[] nums, int n) { int xor = 0; for (int i = 1; i <= n; i++) { xor ^= i; } foreach (int num in nums) { xor ^= num; } return xor; } Follow on: Explanation: XOR all numbers from 1 to n and XOR all elements in array; duplicates cancel out, leaving missing number.
public int FindMissingNumber(int[] nums, int n) {
int xor = 0;
for (int i = 1; i <= n; i++) {
xor ^= i;
}
foreach (int num in nums) {
xor ^= num;
}
return xor;
} Follow on: Explanation: XOR all numbers from 1 to n and XOR all elements in array; duplicates cancel out, leaving missing number.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public int MaximumTotal(IList<IList<int>> triangle) { int n = triangle.Count; int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = triangle[n - 1][i]; for (int layer = n - 2; layer >= 0; layer--) { for (int i = 0; i <= layer; i++) { dp[i] = triangle[layer][i] + Math.Max(dp[i], dp[i + 1]); } } return dp[0]; } Explanation: Start from bottom row, keep updating max path sums up to the top.
public int MaximumTotal(IList<IList<int>> triangle) {
int n = triangle.Count;
int[] dp = new int[n];
for (int i = 0; i < n; i++) dp[i] = triangle[n - 1][i];
for (int layer = n - 2; layer >= 0; layer--) {
for (int i = 0; i <= layer; i++) {
dp[i] = triangle[layer][i] + Math.Max(dp[i], dp[i + 1]);
}
}
return dp[0];
} Explanation: Start from bottom row, keep updating max path sums up to the top.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public bool IsPerfectSquare(int num) { if (num < 0) return false; int left = 0, right = num; while (left <= right) { int mid = left + (right - left) / 2; long sq = (long)mid * mid; if (sq == num) return true; else if (sq < num) left = mid + 1; else right = mid - 1; } return false; } Explanation: Binary search for integer square root and check if square equals num.
public bool IsPerfectSquare(int num) {
if (num < 0) return false;
int left = 0, right = num; while (left <= right) { int mid = left + (right - left) / 2;
long sq = (long)mid * mid;
if (sq == num) return true;
else if (sq < num) left = mid + 1;
else right = mid - 1;
}
return false;
} Explanation: Binary search for integer square root and check if square equals num.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public int CountPartitions(int[] nums) { int sum = 0; foreach (int num in nums) sum += num; if (sum % 2 != 0) return 0; int target = sum / 2; int[] dp = new int[target + 1]; dp[0] = 1; Follow on: foreach (int num in nums) { for (int j = target; j >= num; j--) { dp[j] += dp[j - num]; } } return dp[target]; } Explanation: Classic subset sum DP. dp[j] = ways to get sum j. Counting subsets summing to half the total.
public int CountPartitions(int[] nums) {
int sum = 0;
foreach (int num in nums) sum += num;
if (sum % 2 != 0) return 0;
int target = sum / 2;
int[] dp = new int[target + 1];
dp[0] = 1; Follow on: foreach (int num in nums) {
for (int j = target; j >= num; j--) {
dp[j] += dp[j - num];
}
}
return dp[target];
} Explanation: Classic subset sum DP. dp[j] = ways to get sum j. Counting subsets summing to half the total.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: int LongestPalindromeSubseq(string s) { int n = s.Length; int[,] dp = new int[n, n]; for (int i = n - 1; i >= 0; i--) { dp[i, i] = 1; for (int j = i + 1; j < n; j++) { if (s[i] == s[j]) Follow on: dp[i, j] = dp[i + 1, j - 1] + 2; else dp[i, j] = Math.Max(dp[i + 1, j], dp[i, j - 1]); } } return dp[0, n - 1]; }
int LongestPalindromeSubseq(string s) {
int n = s.Length;
int[,] dp = new int[n, n];
for (int i = n - 1; i >= 0; i--) {
dp[i, i] = 1;
for (int j = i + 1; j < n; j++) {
if (s[i] == s[j]) Follow on: dp[i, j] = dp[i + 1, j - 1] + 2; else dp[i, j] = Math.Max(dp[i + 1, j], dp[i, j - 1]);
}
}
return dp[0, n - 1];
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: List<List<int>> TarjanSCC(Dictionary<int, List<int>> graph) {
int time = 0;
var stack = new Stack<int>();
var onStack = new HashSet<int>();
var low = new Dictionary<int, int>();
var disc = new Dictionary<int, int>();
var visited = new HashSet<int>();
var sccList = new List<List<int>>(); void DFS(int u) { disc[u] = time; Follow on: low[u] = time; time++; stack.Push(u); onStack.Add(u); visited.Add(u); foreach (var v in graph[u]) {
if (!disc.ContainsKey(v)) { DFS(v); low[u] = Math.Min(low[u], low[v]); } else if (onStack.Contains(v)) { low[u] = Math.Min(low[u], disc[v]);
}
}
if (low[u] == disc[u]) {
var scc = new List<int>();
int w; do { w = stack.Pop(); onStack.Remove(w); scc.Add(w); } while (w != u); sccList.Add(scc); }
}
foreach (var node in graph.Keys) {
if (!disc.ContainsKey(node)) DFS(node); }
return sccList;
} Explanation: Tarjan's algorithm finds SCCs using low-link values and DFS stack. Follow on:
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: bool IsIdentical(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null) return false; if (p.val != q.val) return false; return IsIdentical(p.left, q.left) && IsIdentical(p.right, q.right); } Explanation: Recursive check values and structure for equality.
bool IsIdentical(TreeNode p, TreeNode q) { if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return IsIdentical(p.left, q.left) && IsIdentical(p.right, q.right); } Explanation: Recursive check values and structure for equality.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: element in constant time public class MinStack {
private Stack<int> stack = new Stack<int>();
private Stack<int> minStack = new Stack<int>(); Follow on: public void Push(int x) { stack.Push(x); if (minStack.Count == 0 || x <= minStack.Peek()) minStack.Push(x); }
public void Pop() {
if (stack.Peek() == minStack.Peek()) minStack.Pop(); stack.Pop(); }
public int Top() {
return stack.Peek();
}
public int GetMin() {
return minStack.Peek();
}
} Explanation: Use two stacks: one normal stack, one for minimum values. When pushing a smaller or equal value, push it on minStack; when popping, pop minStack if needed.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).