π 1. Two Sum Problem (HashMap)
Write a method that takes an integer array and a target sum. Return the indices of the two numbers that add up to the target.
π Use a Dictionary (HashMap) for an optimal solution.
csharp
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> numMap = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++) {
int diff = target - nums[i];
if (numMap.ContainsKey(diff)) {
return new int[] { numMap[diff], i };
}
numMap[nums[i]] = i;
}
return new int[0]; // No solution
}
πΉ Practice: Try implementing this using nested loops (brute force) and compare performance.
π 2. Reverse a Linked List
Write a function to reverse a singly linked list.
π Understand how pointers work in linked lists.
csharp
public ListNode ReverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
πΉ Practice: Implement recursively as well.
π 3. Find the First Non-Repeating Character in a String
Write a method that returns the first character in a string that does not repeat.
π Use a dictionary for frequency counting.
csharp
public char FirstNonRepeatingChar(string s) {
Dictionary<char, int> count = new Dictionary<char, int>();
foreach (char c in s) count[c] = count.GetValueOrDefault(c, 0) + 1;
foreach (char c in s) if (count[c] == 1) return c;
return '_'; // No non-repeating character
}
πΉ Practice: Handle uppercase/lowercase sensitivity.
π 4. FizzBuzz (Classic)
Print numbers from 1 to n.
If divisible by 3, print "Fizz".
If divisible by 5, print "Buzz".
If divisible by both, print "FizzBuzz".
csharp
public void FizzBuzz(int n) {
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) Console.WriteLine("FizzBuzz");
else if (i % 3 == 0) Console.WriteLine("Fizz");
else if (i % 5 == 0) Console.WriteLine("Buzz");
else Console.WriteLine(i);
}
}
πΉ Practice: Solve this using a switch expression in C#.
πΎ 5. Implement a Stack Using Two Queues
Implement a stack (LIFO) using only two queues (FIFO).
π Understand how stacks & queues differ.
csharp
using System.Collections.Generic;
public class MyStack {
private Queue<int> q1 = new Queue<int>();
private Queue<int> q2 = new Queue<int>();
public void Push(int x) {
q2.Enqueue(x);
while (q1.Count > 0) q2.Enqueue(q1.Dequeue());
var temp = q1;
q1 = q2;
q2 = temp;
}
public int Pop() => q1.Dequeue();
public int Top() => q1.Peek();
public bool Empty() => q1.Count == 0;
}
πΉ Practice: Implement using a single queue.
π³ 6. Binary Search Tree: Find Depth
Write a function to find the depth of a binary tree.
csharp
public int MaxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.Max(MaxDepth(root.left), MaxDepth(root.right));
}
πΉ Practice: Implement an iterative BFS solution.
π§ 7. OOP: Design a Parking System
Design a Parking System with the following classes:
ParkingSystem:
AddCar(int carType): Checks if there's space for a car.
Uses big, medium, and small slots.
csharp
public class ParkingSystem {
private int[] slots;
public ParkingSystem(int big, int medium, int small) {
slots = new int[] { big, medium, small };
}
public bool AddCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 1]--;
return true;
}
return false;
}
}
πΉ Practice: Extend with dynamic pricing.
β‘ 8. Implement an LRU Cache
Implement a Least Recently Used (LRU) Cache using a Dictionary + LinkedList.
csharp
using System.Collections.Generic;
public class LRUCache {
private int capacity;
private Dictionary<int, LinkedListNode<(int key, int value)>> cache;
private LinkedList<(int key, int value)> order;
public LRUCache(int capacity) {
this.capacity = capacity;
cache = new Dictionary<int, LinkedListNode<(int key, int value)>>();
order = new LinkedList<(int key, int value)>();
}
public int Get(int key) {
if (!cache.ContainsKey(key)) return -1;
var node = cache[key];
order.Remove(node);
order.AddFirst(node);
return node.Value.value;
}
public void Put(int key, int value) {
if (cache.ContainsKey(key)) order.Remove(cache[key]);
if (cache.Count == capacity) {
cache.Remove(order.Last.Value.key);
order.RemoveLast();
}
order.AddFirst((key, value));
cache[key] = order.First;
}
}
πΉ Practice: Extend it with time-based expiry.
π’ 9. Count Distinct Elements in an Array
Count the number of distinct elements in an array. π Use a HashSet for efficiency.
csharp
public int CountDistinct(int[] nums) {
return new HashSet<int>(nums).Count;
}
πΉ Practice: Do it without HashSet.
π 10. Rotate an Array
Write a function to rotate an array k times to the right.
π Use an efficient approach (O(n) time, O(1) space).
csharp
public void Rotate(int[] nums, int k) {
k %= nums.Length;
Array.Reverse(nums, 0, nums.Length);
Array.Reverse(nums, 0, k);
Array.Reverse(nums, k, nums.Length - k);
}
πΉ Practice: Solve without modifying the original array.
Next Steps
πΉ Time yourself while solving these.
πΉ Use unit tests to verify your solutions.
πΉ Try solving in multiple ways (brute force, optimized).
Top comments (0)