Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: 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. headB : a.n…
Short answer: List<List<string>> SolveNQueens(int n) List<string> GenerateBoard(int[] board, int n) Example code { List<List<string>> results = new List<List<string>>(); int[] bo…
Short answer: int FindPeakElement(int[] nums) { int left = 0, right = nums.Length - 1; while (left < right) { int mid = (left + right) / 2; if (nums[mid] > nums[mid + 1]) right = mid; else left = mid + 1; } return…
Short answer: int LIS(int[] nums) { int n = nums.Length; int[] dp = new int[n]; Array.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.…
Short answer: Logic Track highest and second highest in one loop. int[] arr = { 10, 5, 20, 8 }; int first = int.MinValue, second = int.MinValue; foreach (int num in arr) { if (num > first) { second = first; first = nu…
Short answer: ^= b; b ^= a; ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. Explain a bit more (Check a != b to avoid zeroing when both are same.) ^= b; b ^= a; ^= b; } } Explanation: XO…
Short answer: = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm. Explain a bit more LCM calculated via LCM(a,b) = (a*b)/GCD(a,b). = temp; } retur…
Short answer: public void Swap(ref int a, ref int b) { if (a != b) { a ^= b; b ^= a; a ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.)…
Short answer: public int FindCelebrity(int n, Func<int, int, bool> knows) { int candidate = 0; for (int i = 1; i < n; i++) { if (knows(candidate, i)) candidate = i; } for (int i = 0; i < n; i++) { if (i != ca…
Short answer: public int GCD(int a, int b) { Follow on: while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algor…
Short answer: appears twice except one public int SingleNumber(int[] nums) { Example code int result = 0; foreach (var num in nums) { result ^= num; } return result; } Explanation: XOR of all elements cancels duplicates,…
Short answer: int MinPathSum(int[][] grid) { int m = grid.Length, n = grid[0].Length; for (int i = 1; i < m; i++) grid[i][0] += grid[i - 1][0]; for (int j = 1; j < n; j++) grid[0][j] += grid[0][j - 1]; for (int i =…
Short answer: int[] Dijkstra(Dictionary<int, List<(int neighbor, int weight)>> graph, int source, int vertices) { int[] dist = new int[vertices]; for (int i = 0; i < vertices; i++) dist[i] = int.MaxValue;…
Short answer: TreeNode LCA(TreeNode root, int n1, int n2) { if (root == null) return null; if (root.val == n1 || root.val == n2) return root; TreeNode left = LCA(root.left, n1, n2); Follow on: TreeNode right = LCA(root.r…
Short answer: 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…
Short answer: 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; Fo…
Short 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 substri…
Short answer: 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 unvis…
Short 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. Example code int SumOfDigits(int n) { i…
Short answer: Follow on: int BinarySearch(int[] arr, int target) Example code { int low = 0, high = arr.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (…
Short answer: Amount) int CoinChange(int[] coins, int amount) Example code { int[] dp = new int[amount + 1]; Array.Fill(dp, amount + 1); dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if…
Short answer: 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…
Short answer: 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 signific…
Short answer: 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(str…
Short 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. E…
C# Coding Interview C# Programming Tutorial · Coding
Short answer: 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. 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. 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. 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. 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. headB : a.next; b = (b == null) ? headA : b.next; return a; // either intersection…
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<string>> SolveNQueens(int n) List<string> GenerateBoard(int[] board, int n)
{
List<List<string>> results = new List<List<string>>();
int[] board = new int[n]; // board[i] = column position of queen in row i Solve(0, board, results, n); return results;
} void Solve(int row, int[] board, List<List<string>> results, int n) {
if (row == n)
{ results.Add(GenerateBoard(board, n)); return;
}
for (int col = 0; col < n; col++)
{
if (IsSafe(row, col, board))
{
board[row] = col; Solve(row + 1, board, results, n); }
}
} bool IsSafe(int row, int col, int[] board) {
for (int i = 0; i < row; i++) Follow on: {
if (board[i] == col || Math.Abs(board[i] - col) == Math.Abs(i - row)) return false;
}
return true;
}
{
List<string> res = new List<string>();
for (int i = 0; i < n; i++)
{
char[] row = new char[n];
for (int j = 0; j < n; j++)
row[j] = '.';
row[board[i]] = 'Q'; res.Add(new string(row)); }
return res;
} Explanation: Backtracking places queens row by row while checking columns and diagonals.
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 FindPeakElement(int[] nums) { int left = 0, right = nums.Length - 1; while (left < right) { int mid = (left + right) / 2; if (nums[mid] > nums[mid + 1]) right = mid; else left = mid + 1; } return left; } Explanation: Binary search comparing mid element with right neighbor to find peak.
int FindPeakElement(int[] nums)
{
int left = 0, right = nums.Length - 1; while (left < right) {
int mid = (left + right) / 2;
if (nums[mid] > nums[mid + 1])
right = mid; else left = mid + 1;
}
return left;
} Explanation: Binary search comparing mid element with right neighbor to find peak.
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 LIS(int[] nums) { int n = nums.Length; int[] dp = new int[n]; Array.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:
int LIS(int[] nums)
{
int n = nums.Length;
int[] dp = new int[n]; Array.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:
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 Track highest and second highest in one loop. int[] arr = { 10, 5, 20, 8 }; int first = int.MinValue, second = int.MinValue; foreach (int num in arr) { if (num > first) { second = first; first = num; } else if (num > second && num != first) { second = num; } }
Logic Track highest and second highest in one loop. int[] arr = { 10, 5, 20, 8 };
int first = int.MinValue, second = int.MinValue;
foreach (int num in arr)
{
if (num > first)
{
second = first;
first = num;
} else if (num > second && num != first) {
second = 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: ^= b; b ^= a; ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage.
(Check a != b to avoid zeroing when both are same.) ^= b; b ^= a; ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.) ^= b; b ^= a; ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.) ^= b; b ^= a; ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.)
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: = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm.
LCM calculated via LCM(a,b) = (a*b)/GCD(a,b). = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b). = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b). = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b).
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 void Swap(ref int a, ref int b) { if (a != b) { a ^= b; b ^= a; a ^= b; } } Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.)
public void Swap(ref int a, ref int b) {
if (a != b) {
a ^= b;
b ^= a;
a ^= b;
}
} Explanation: XOR swap algorithm exchanges values without extra storage. (Check a != b to avoid zeroing when both are same.)
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 FindCelebrity(int n, Func<int, int, bool> knows) { int candidate = 0; for (int i = 1; i < n; i++) { if (knows(candidate, i)) candidate = i; } for (int i = 0; i < n; i++) { if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) return -1; } return candidate; } Explanation: First find candidate by elimination, then verify candidate. Follow on:
public int FindCelebrity(int n, Func<int, int, bool> knows) {
int candidate = 0;
for (int i = 1; i < n; i++) {
if (knows(candidate, i)) candidate = i;
}
for (int i = 0; i < n; i++) {
if (i != candidate && (knows(candidate, i) || !knows(i, candidate))) return -1;
}
return candidate;
} Explanation: First find candidate by elimination, then verify candidate. 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 GCD(int a, int b) { Follow on: while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } public int LCM(int a, int b) { return (a / GCD(a, b)) * b; } Explanation: GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b).
public int GCD(int a, int b) { Follow on: while (b != 0) { int temp = b;
b = a % b;
a = temp;
}
return a;
}
public int LCM(int a, int b) {
return (a / GCD(a, b)) * b;
} Explanation: GCD uses Euclidean algorithm. LCM calculated via LCM(a,b) = (a*b)/GCD(a,b).
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: appears twice except one public int SingleNumber(int[] nums) {
int result = 0;
foreach (var num in nums) {
result ^= num;
}
return result;
} Explanation: XOR of all elements cancels duplicates, leaving the single unique 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: int MinPathSum(int[][] grid) { int m = grid.Length, n = grid[0].Length; for (int i = 1; i < m; i++) grid[i][0] += grid[i - 1][0]; for (int j = 1; j < n; j++) grid[0][j] += grid[0][j - 1]; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { grid[i][j] += Math.Min(grid[i - 1][j], grid[i][j - 1]); } } return grid[m - 1][n - 1]; }
int MinPathSum(int[][] grid) {
int m = grid.Length, n = grid[0].Length;
for (int i = 1; i < m; i++) grid[i][0] += grid[i - 1][0];
for (int j = 1; j < n; j++) grid[0][j] += grid[0][j - 1];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
grid[i][j] += Math.Min(grid[i - 1][j], grid[i][j - 1]);
}
}
return grid[m - 1][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: int[] Dijkstra(Dictionary<int, List<(int neighbor, int weight)>> graph, int source, int vertices) { int[] dist = new int[vertices]; for (int i = 0; i < vertices; i++) dist[i] = int.MaxValue; dist[source] = 0; var pq = new SortedSet<(int dist, int node)>(); pq.Add((0, source)); while (pq.Count > 0) { var current = pq.Min; pq.Remove(current); int u = current.node; foreach (var (v, w) in graph[u]) { if (dist[u] + w <…
dist[v]) { if (dist[v] != int.MaxValue) pq.Remove((dist[v], v)); dist[v] = dist[u] + w; pq.Add((dist[v], v)); } } } return dist; } Explanation: Uses a priority queue to pick node with min dist; relax edges.
int[] Dijkstra(Dictionary<int, List<(int neighbor, int weight)>> graph, int source, int vertices) { int[] dist = new int[vertices];
for (int i = 0; i < vertices; i++) dist[i] = int.MaxValue;
dist[source] = 0;
var pq = new SortedSet<(int dist, int node)>(); pq.Add((0, source)); while (pq.Count > 0) { var current = pq.Min; pq.Remove(current); int u = current.node;
foreach (var (v, w) in graph[u]) {
if (dist[u] + w < dist[v]) {
if (dist[v] != int.MaxValue) pq.Remove((dist[v], v)); dist[v] = dist[u] + w; pq.Add((dist[v], v)); }
}
}
return dist;
} Explanation: Uses a priority queue to pick node with min dist; relax edges.
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 LCA(TreeNode root, int n1, int n2) { if (root == null) return null; if (root.val == n1 || root.val == n2) return root; TreeNode left = LCA(root.left, n1, n2); Follow on: TreeNode right = LCA(root.right, n1, n2); if (left != null && right != null) return root; return left ??
right; } int FindLevel(TreeNode root, int val, int level) { if (root == null) return -1; if (root.val == val) return level; int left = FindLevel(root.left, val, level + 1); if (left != -1) return left; return FindLevel(root.right, val, level + 1); } int DistanceBetweenNodes(TreeNode root, int n1, int n2) { TreeNode lca = LCA(root, n1, n2); int d1 = FindLevel(lca, n1, 0); int d2 = FindLevel(lca, n2, 0); return d1 + d2; } Explanation: Find Lowest Common Ancestor (LCA) then sum distances from LCA to each node.
TreeNode LCA(TreeNode root, int n1, int n2) { if (root == null) return null;
if (root.val == n1 || root.val == n2) return root;
TreeNode left = LCA(root.left, n1, n2); Follow on: TreeNode right = LCA(root.right, n1, n2);
if (left != null && right != null) return root;
return left ?? right;
}
int FindLevel(TreeNode root, int val, int level) {
if (root == null) return -1;
if (root.val == val) return level;
int left = FindLevel(root.left, val, level + 1);
if (left != -1) return left;
return FindLevel(root.right, val, level + 1);
}
int DistanceBetweenNodes(TreeNode root, int n1, int n2) {
TreeNode lca = LCA(root, n1, n2);
int d1 = FindLevel(lca, n1, 0);
int d2 = FindLevel(lca, n2, 0);
return d1 + d2;
} Explanation: Find Lowest Common Ancestor (LCA) then sum distances from LCA to each node.
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: 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)
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
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.
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: 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. 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.
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 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.
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 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: 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.
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;
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 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.
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 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: 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.
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: Amount) int CoinChange(int[] coins, int amount)
{
int[] dp = new int[amount + 1]; Array.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:
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: 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;
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;
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 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:
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:
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 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.
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.
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 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.
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 coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).