Interview Q&A

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

4616 total questions 4516 technical 100 career & HR 4346 from PDF library

Showing 76–100 of 140

Popular tracks

Mid PDF
Implement sliding window maximum (using a deque)?

public int[] MaxSlidingWindow(int[] nums, int k) { if (nums == null || k <= 0) return new int[0]; int n = nums.Length; int[] result = new int[n - k + 1]; LinkedList<int> deque = new LinkedList<int>(); // s…

Coding Read answer
Mid PDF
Add two numbers represented by linked lists (each node contains a digit) ListNode AddTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode curr = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int x = (l1 != null) ?

l1.val : 0; int y = (l2 != null) ? l2.val : 0; int sum = x + y + carry; carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; if (l1 != null) l1 = l1.next; if (l2 != null) l2 = l2.next; Follow on: retur…

Coding Read answer
Mid PDF
Check if a String is Rotation of Another String?

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…

Coding Read answer
Mid PDF
Find Number of Islands in 2D Matrix?

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: {…

Coding Read answer
Mid PDF
Sum of Digits of a Number?

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. What interviewers expect A clear definiti…

Coding Read answer
Mid PDF
Binary Search?

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 =…

Coding Read answer
Mid PDF
Coin Change Problem (Minimum Coins to Make?

mount) int CoinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; rray.Fill(dp, amount + 1); dp[0] = 0; for (int i = 1; i <= amount; i++) { foreach (int coin in coins) { if (coin <= i) dp[i] = Math.M…

Coding Read answer
Mid PDF
Count occurrence of each word?

string sentence = "dotnet is great dotnet is powerful"; string[] words = sentence.Split(' '); Dictionary<string, int> map = new Dictionary<string, int>(); foreach (string word in words) map[word] = map.Contai…

Coding Scenarios Read answer
Mid PDF
Reverse the bits of a number?

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,…

Coding Read answer
Mid PDF
Implement a trie (prefix tree) for string matching?

public class TrieNode { public TrieNode[] Children = new TrieNode[26]; public bool IsEnd = false; } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } public void Insert(string word) { Tr…

Coding Read answer
Mid PDF
Find number of trailing zeroes in factorial of a number?

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. Wha…

Coding Read answer
Mid PDF
Search for a range (find start and end indices of target in sorted array)?

public int[] SearchRange(int[] nums, int target) { int left = FindBoundary(nums, target, true); int right = FindBoundary(nums, target, false); return new int[] { left, right }; } private int FindBoundary(int[] nums, int…

Coding Read answer
Mid PDF
Edit Distance (Levenshtein Distance)?

int EditDistance(string word1, string word2) { int m = word1.Length, n = word2.Length; int[,] dp = new int[m + 1, n + 1]; for (int i = 0; i <= m; i++) dp[i, 0] = i; for (int j = 0; j <= n; j++) dp[0, j] = j; for (i…

Coding Read answer
Mid PDF
Print nodes at a specific level (BFS)?

void PrintNodesAtLevel(Dictionary<int, List<int>> graph, int start, int targetLevel) { var visited = new HashSet<int>(); var queue = new Queue<(int node, int level)>(); Follow on: queue.Enqueue((s…

Coding Read answer
Mid PDF
Level-order traversal but return values in reverse order?

List<List<int>> LevelOrderBottom(TreeNode root) { var res = new List<List<int>>(); if (root == null) return res; Queue<TreeNode> queue = new Queue<TreeNode>(); queue.Enqueue(root); whi…

Coding Read answer
Mid PDF
Flatten a linked list with next and child pointers?

public class MultiLevelNode { public int val; public MultiLevelNode next; public MultiLevelNode child; public MultiLevelNode(int x) { val = x; next = null; child = null; } } MultiLevelNode Flatten(MultiLevelNode head) {…

Coding Read answer
Mid PDF
Find the Smallest Window in String s that Contains?

ll Characters of String t string MinWindow(string s, string t) { if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return ""; Dictionary<char, int> dictT = new Dictionary<char, int>(); foreach (char c i…

Coding Read answer
Mid PDF
Find Longest Consecutive Sequence in an Unsorted?

rray int LongestConsecutive(int[] nums) { HashSet<int> set = new HashSet<int>(nums); int longest = 0; foreach (int num in set) { if (!set.Contains(num - 1)) Follow on: { int currentNum = num; int length = 1;…

Coding Read answer
Mid PDF
Count Trailing Zeroes in Factorial of a Number?

Answer: int TrailingZeroes(int n) { int count = 0; for (int i = 5; i <= n; i *= 5) { count += n / i; } return count; } Explanation: Trailing zeros come from factors of 10 = 2 × 5, but 2s are plenty, count 5s. What…

Coding Read answer
Mid PDF
Find Element that Appears Once in Sorted Array?

(Others Appear Twice) int SingleNonDuplicate(int[] nums) { int low = 0, high = nums.Length - 1; while (low < high) { int mid = low + (high - low) / 2; if (mid % 2 == 1) mid--; // ensure mid is even if (nums[mid] == nu…

Coding Read answer
Mid PDF
Word Break Problem?

bool WordBreak(string s, HashSet<string> wordDict) { bool[] dp = new bool[s.Length + 1]; dp[0] = true; for (int i = 1; i <= s.Length; i++) { for (int j = 0; j < i; j++) { if (dp[j] && wordDict.Contain…

Coding Read answer
Mid PDF
Find all duplicate characters in a string?

string input = "programming"; Dictionary<char, int> map = new Dictionary<char, int>(); foreach (char c in input) map[c] = map.ContainsKey(c) ? map[c] + 1 : 1; foreach (var item in map) { if (item.Value > 1…

Coding Scenarios Read answer
Mid PDF
Find Majority Element (More than n/2 times)?

int MajorityElement(int[] nums) { int count = 0, candidate = 0; foreach (var num in nums) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; } return candidate; } Explanation: Boyer-Moore Voting Alg…

Coding Read answer
Mid PDF
Power of a Number (x^n) — Fast Exponentiation?

double Power(double x, int n) Follow on: { if (n == 0) return 1; double temp = Power(x, n / 2); if (n % 2 == 0) return temp * temp; else return (n > 0) ? x * temp * temp : (temp * temp) / x; } Explanation: Recursive f…

Coding Read answer
Mid PDF
Find Majority Element (Boyer-Moore Voting?

lgorithm) int MajorityElement(int[] nums) { int count = 0, candidate = 0; foreach (var num in nums) { if (count == 0) candidate = num; count += (num == candidate) ? 1 : -1; } return candidate; } Explanation: Maintain a c…

Coding Read answer

C# Coding Interview C# Programming Tutorial · Coding

public int[] MaxSlidingWindow(int[] nums, int k) {

if (nums == null || k <= 0) return new int[0];

int n = nums.Length;

int[] result = new int[n - k + 1];

LinkedList<int> deque = new LinkedList<int>(); // store indices

for (int i = 0; i < n; i++) {

// Remove indices out of window

if (deque.Count > 0 && deque.First.Value <= i - k)

Follow on:

deque.RemoveFirst();

// Remove smaller values from the back

while (deque.Count > 0 && nums[deque.Last.Value] < nums[i])

deque.RemoveLast();

deque.AddLast(i);

if (i >= k - 1)

result[i - k + 1] = nums[deque.First.Value];

return result;

Explanation:

Use a deque to keep indexes of useful elements in current window, ensuring the front is

always max.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

l1.val : 0;

int y = (l2 != null) ? l2.val : 0;

int sum = x + y + carry;

carry = sum / 10;

curr.next = new ListNode(sum % 10);

curr = curr.next;

if (l1 != null) l1 = l1.next;

if (l2 != null) l2 = l2.next;

Follow on:

return dummy.next;

Explanation:

Add digit by digit with carry, creating new nodes for the result.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Answer: bool IsRotation(string s1, string s2) { if (s1.Length != s2.Length) return false; string doubled = s1 + s1; return doubled.Contains(s2); } Follow on: Explanation: If s2 is rotation of s1, it must be substring of s1+s1.

What interviewers expect

  • A clear definition tied to Coding in C# Coding Interview projects
  • Trade-offs (performance, maintainability, security, cost)
  • When you would and would not use it in production

Real-world example

In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.

How to explain in the interview

  1. Define the concept in one or two sentences.
  2. Context — where it fits in C# Coding Interview architecture.
  3. Example — a specific project, bug, or performance win.
  4. Trade-off — what you gain vs what you sacrifice.

Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

int NumIslands(char[][] grid)
{
if (grid == null || grid.Length == 0) return 0;
int count = 0;
for (int i = 0; i < grid.Length; i++)
{
for (int j = 0; j < grid[0].Length; j++)
{
if (grid[i][j] == '1')

Follow on:

{

DFS(grid, i, j);

count++;

}
}
}
return count;
}

void DFS(char[][] grid, int i, int j)

{
if (i < 0 || j < 0 || i >= grid.Length || j >= grid[0].Length ||

grid[i][j] == '0')

return;
grid[i][j] = '0'; // Mark visited

DFS(grid, i + 1, j);

DFS(grid, i - 1, j);

DFS(grid, i, j + 1);

DFS(grid, i, j - 1);

}

Explanation:

Use DFS to mark all connected land cells, count islands by visiting unvisited lands.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Answer: int SumOfDigits(int n) { int sum = 0; n = Math.Abs(n); while (n &gt; 0) { sum += n % 10; n /= 10; } return sum; } Explanation: Extract digits using modulo 10 and add.

What interviewers expect

  • A clear definition tied to Coding in C# Coding Interview projects
  • Trade-offs (performance, maintainability, security, cost)
  • When you would and would not use it in production

Real-world example

In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.

How to explain in the interview

  1. Define the concept in one or two sentences.
  2. Context — where it fits in C# Coding Interview architecture.
  3. Example — a specific project, bug, or performance win.
  4. Trade-off — what you gain vs what you sacrifice.

Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Follow on:

int BinarySearch(int[] arr, int target)
{
int low = 0, high = arr.Length - 1;

while (low <= high)

{
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;
}
return -1;
}

Explanation:

Standard binary search to find target’s index or -1 if not found.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

mount)

int CoinChange(int[] coins, int amount)
{
int[] dp = new int[amount + 1];

rray.Fill(dp, amount + 1);

dp[0] = 0;
for (int i = 1; i <= amount; i++)
{
foreach (int coin in coins)
{
if (coin <= i)
dp[i] = Math.Min(dp[i], 1 + dp[i - coin]);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}

Explanation:

Bottom-up DP: min coins needed for all amounts up to target.

Follow on:

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding Scenarios

string sentence = "dotnet is great dotnet is powerful";
string[] words = sentence.Split(' ');
Dictionary<string, int> map = new Dictionary<string, int>();
foreach (string word in words)
map[word] = map.ContainsKey(word) ? map[word] + 1 : 1;
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

public uint ReverseBits(uint n) {
uint result = 0;
for (int i = 0; i < 32; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}

Explanation:

Iteratively take least significant bit of n, add it to result’s LSB, shift both accordingly.

Follow on:

Follow on:

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

public class TrieNode {
public TrieNode[] Children = new TrieNode[26];
public bool IsEnd = false;
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void Insert(string word) {
TrieNode node = root;
foreach (char c in word) {
int idx = c - 'a';
if (node.Children[idx] == null)
node.Children[idx] = new TrieNode();
node = node.Children[idx];
}
node.IsEnd = true;
}
public bool Search(string word) {
TrieNode node = SearchNode(word);
return node != null && node.IsEnd;
}
public bool StartsWith(string prefix) {
return SearchNode(prefix) != null;
}
private TrieNode SearchNode(string word) {
TrieNode node = root;
foreach (char c in word) {
int idx = c - 'a';
if (node.Children[idx] == null) return null;

Follow on:

node = node.Children[idx];
}
return node;
}
}

Explanation:

Standard trie with insert, search, and prefix checking.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Answer: public int TrailingZeroes(int n) { int count = 0; while (n &gt; 0) { n /= 5; count += n; } return count; } Explanation: Count factors of 5 in factorial since 2s are plentiful, trailing zeros depend on 5s.

What interviewers expect

  • A clear definition tied to Coding in C# Coding Interview projects
  • Trade-offs (performance, maintainability, security, cost)
  • When you would and would not use it in production

Real-world example

In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.

How to explain in the interview

  1. Define the concept in one or two sentences.
  2. Context — where it fits in C# Coding Interview architecture.
  3. Example — a specific project, bug, or performance win.
  4. Trade-off — what you gain vs what you sacrifice.

Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

public int[] SearchRange(int[] nums, int target) {
int left = FindBoundary(nums, target, true);
int right = FindBoundary(nums, target, false);
return new int[] { left, right };
}
private int FindBoundary(int[] nums, int target, bool findFirst) {

Follow on:

int left = 0, right = nums.Length - 1;
int boundary = -1;

while (left <= right) {

int mid = left + (right - left) / 2;
if (nums[mid] == target) {
boundary = mid;
if (findFirst)
right = mid - 1;

else

left = mid + 1;
}
else if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return boundary;
}

Explanation:

Binary search twice — once to find first occurrence and once for last occurrence.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

int EditDistance(string word1, string word2) {
int m = word1.Length, n = word2.Length;
int[,] dp = new int[m + 1, n + 1];
for (int i = 0; i <= m; i++) dp[i, 0] = i;
for (int j = 0; j <= n; j++) dp[0, j] = j;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (word1[i - 1] == word2[j - 1])
dp[i, j] = dp[i - 1, j - 1];

else

Follow on:

dp[i, j] = 1 + Math.Min(dp[i - 1, j - 1],

Math.Min(dp[i - 1, j], dp[i, j - 1]));

}
}
return dp[m, n];
}
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

void PrintNodesAtLevel(Dictionary<int, List<int>> graph, int start,

int targetLevel) {
var visited = new HashSet<int>();
var queue = new Queue<(int node, int level)>();

Follow on:

queue.Enqueue((start, 0));

visited.Add(start);

while (queue.Count > 0) {

var (node, level) = queue.Dequeue();
if (level == targetLevel) {

Console.WriteLine(node);

}
if (level > targetLevel) break;
foreach (var neighbor in graph[node]) {
if (!visited.Contains(neighbor)) {

visited.Add(neighbor);

queue.Enqueue((neighbor, level + 1));

}
}
}
}

Explanation:

BFS traversal with level tracking; print nodes at the requested level.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

List<List<int>> LevelOrderBottom(TreeNode root) {
var res = new List<List<int>>();
if (root == null) return res;
Queue<TreeNode> queue = new Queue<TreeNode>();

queue.Enqueue(root);

while (queue.Count > 0) {

int size = queue.Count;
var level = new List<int>();

Follow on:

for (int i = 0; i < size; i++) {
TreeNode node = queue.Dequeue();

level.Add(node.val);

if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}

res.Insert(0, level); // prepend to get reverse order

}
return res;
}

Explanation:

Perform normal BFS, insert each level at front of result list for reversed order.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

public class MultiLevelNode {
public int val;
public MultiLevelNode next;
public MultiLevelNode child;
public MultiLevelNode(int x) { val = x; next = null; child =

null; }

}

MultiLevelNode Flatten(MultiLevelNode head) {

if (head == null) return null;
MultiLevelNode dummy = new MultiLevelNode(0);
MultiLevelNode prev = dummy;
Stack<MultiLevelNode> stack = new Stack<MultiLevelNode>();

stack.Push(head);

while (stack.Count > 0) {

var curr = stack.Pop();
prev.next = curr;
curr.child = null; // remove child pointer after flattening
prev = curr;
if (curr.next != null) stack.Push(curr.next);
if (curr.child != null) stack.Push(curr.child);
}
return dummy.next;
}

Follow on:

Explanation:

Use a stack to perform DFS; attach nodes and remove child pointers.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

ll Characters of String t

string MinWindow(string s, string t)
{
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return

"";

Dictionary<char, int> dictT = new Dictionary<char, int>();
foreach (char c in t)
dictT[c] = dictT.ContainsKey(c) ? dictT[c] + 1 : 1;
int required = dictT.Count;
int formed = 0;
Dictionary<char, int> windowCounts = new Dictionary<char,
int>();
int left = 0, right = 0;
int minLen = int.MaxValue, minLeft = 0;

while (right < s.Length)

{
char c = s[right];

windowCounts[c] = windowCounts.ContainsKey(c) ?

windowCounts[c] + 1 : 1;

if (dictT.ContainsKey(c) && windowCounts[c] == dictT[c])
formed++;

while (left <= right && formed == required)

{
if (right - left + 1 < minLen)
{
minLen = right - left + 1;

Follow on:

minLeft = left;
}
char leftChar = s[left];

windowCounts[leftChar]--;

if (dictT.ContainsKey(leftChar) &&

windowCounts[leftChar] < dictT[leftChar])

formed--;

left++;

}

right++;

}
return minLen == int.MaxValue ? "" : s.Substring(minLeft,

minLen);

}

Explanation:

Sliding window with two pointers keeps track of counts of chars matching the target.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

rray

int LongestConsecutive(int[] nums)
{
HashSet<int> set = new HashSet<int>(nums);
int longest = 0;
foreach (int num in set)
{
if (!set.Contains(num - 1))

Follow on:

{
int currentNum = num;
int length = 1;

while (set.Contains(currentNum + 1))

{

currentNum++;

length++;

}
longest = Math.Max(longest, length);
}
}
return longest;
}

Explanation:

Check only starts of sequences, count consecutive numbers using HashSet for O(n).

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

Answer: int TrailingZeroes(int n) { int count = 0; for (int i = 5; i &lt;= n; i *= 5) { count += n / i; } return count; } Explanation: Trailing zeros come from factors of 10 = 2 × 5, but 2s are plenty, count 5s.

What interviewers expect

  • A clear definition tied to Coding in C# Coding Interview projects
  • Trade-offs (performance, maintainability, security, cost)
  • When you would and would not use it in production

Real-world example

In a production C# Coding Interview application, teams apply this when handling user-facing features or integration boundaries. For example, you might use it during a sprint where reliability and observability matter—logging metrics, validating edge cases, and documenting the decision in an ADR so future developers understand why the approach was chosen.

How to explain in the interview

  1. Define the concept in one or two sentences.
  2. Context — where it fits in C# Coding Interview architecture.
  3. Example — a specific project, bug, or performance win.
  4. Trade-off — what you gain vs what you sacrifice.

Tip: Practice aloud on Toolliyo mock interview or the Interview Q&A section before your real interview.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

(Others Appear Twice)

int SingleNonDuplicate(int[] nums)
{
int low = 0, high = nums.Length - 1;

while (low < high)

{
int mid = low + (high - low) / 2;
if (mid % 2 == 1) mid--; // ensure mid is even
if (nums[mid] == nums[mid + 1])
low = mid + 2;

else

high = mid;

Follow on:

}
return nums[low];
}

Explanation:

Pairs appear consecutively; use binary search on even indices to find mismatch.

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

bool WordBreak(string s, HashSet<string> wordDict)

{
bool[] dp = new bool[s.Length + 1];
dp[0] = true;
for (int i = 1; i <= s.Length; i++)
{
for (int j = 0; j < i; j++)
{
if (dp[j] && wordDict.Contains(s.Substring(j, i - j)))
{
dp[i] = true;

break;

}
}
}
return dp[s.Length];
}

Explanation:

DP to check if substring can be segmented using dictionary words.

Follow on:

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding Scenarios

string input = "programming";
Dictionary<char, int> map = new Dictionary<char, int>();
foreach (char c in input)
map[c] = map.ContainsKey(c) ? map[c] + 1 : 1;
foreach (var item in map)
{
if (item.Value > 1)

Console.WriteLine(item.Key);

}
Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

int MajorityElement(int[] nums)
{
int count = 0, candidate = 0;
foreach (var num in nums)
{
if (count == 0)
candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}

Explanation:

Boyer-Moore Voting Algorithm tracks majority element by counting net votes.

Follow on:

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

double Power(double x, int n)

Follow on:

{
if (n == 0) return 1;
double temp = Power(x, n / 2);
if (n % 2 == 0)
return temp * temp;

else

return (n > 0) ? x * temp * temp : (temp * temp) / x;
}

Explanation:

Recursive fast power divides exponent by 2 to reduce complexity to O(log n).

Permalink & share

C# Coding Interview C# Programming Tutorial · Coding

lgorithm)

int MajorityElement(int[] nums)
{
int count = 0, candidate = 0;
foreach (var num in nums)
{
if (count == 0)
candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}

Explanation:

Maintain a candidate and count; majority element survives this cancellation.

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