DEV Community

Jasper Aurelio Villas
Jasper Aurelio Villas

Posted on

Technical Assessment

πŸ›  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
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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;
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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);
    }
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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;
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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));
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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;
    }
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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;
    }
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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;
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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);
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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)