Challenge - 5 Problems
Master of Lookup Structures
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateLookup Time Comparison: Array vs Linked List
What is the output of the following C# code that searches for the number 5 in an array and a linked list?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {1, 3, 5, 7, 9}; LinkedList<int> list = new LinkedList<int>(new int[] {1, 3, 5, 7, 9}); bool foundInArray = false; foreach (int num in arr) { if (num == 5) { foundInArray = true; break; } } bool foundInList = false; foreach (int num in list) { if (num == 5) { foundInList = true; break; } } Console.WriteLine($"Found in array: {foundInArray}"); Console.WriteLine($"Found in linked list: {foundInList}"); } }
Attempts:
2 left
💡 Hint
Both array and linked list contain the number 5, so searching should find it.
✗ Incorrect
The code searches for the number 5 in both the array and the linked list. Since both contain 5, the output shows True for both.
❓ Predict Output
intermediateHash Map Lookup Result
What will be printed after running this C# code that uses a Dictionary to lookup a key?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> map = new Dictionary<string, int>() { {"apple", 1}, {"banana", 2}, {"cherry", 3} }; if (map.TryGetValue("banana", out int value)) { Console.WriteLine(value); } else { Console.WriteLine("Not found"); } } }
Attempts:
2 left
💡 Hint
Check the value associated with the key "banana" in the dictionary.
✗ Incorrect
The dictionary contains "banana" with value 2, so TryGetValue returns true and prints 2.
🧠 Conceptual
advancedTime Complexity of Lookup Operations
Which data structure provides the fastest average lookup time for searching an element by key?
Attempts:
2 left
💡 Hint
Consider average time complexity for each data structure's lookup.
✗ Incorrect
Hash maps provide average O(1) lookup time, which is faster than linear search in arrays or linked lists.
❓ Predict Output
advancedEffect of Collisions in Hash Map Lookup
What will be the output of this C# code that inserts keys causing collisions in a Dictionary and then looks up a key?
DSA C#
using System; using System.Collections.Generic; class Program { class BadHash { public int Value; public BadHash(int v) { Value = v; } public override int GetHashCode() { return 1; } // Forces collision public override bool Equals(object obj) { if (obj is BadHash other) return Value == other.Value; return false; } } static void Main() { Dictionary<BadHash, string> map = new Dictionary<BadHash, string>(); map[new BadHash(1)] = "One"; map[new BadHash(2)] = "Two"; map[new BadHash(3)] = "Three"; Console.WriteLine(map[new BadHash(2)]); } }
Attempts:
2 left
💡 Hint
Even with collisions, dictionary uses Equals to find the correct key.
✗ Incorrect
Despite all keys having the same hash code, the dictionary uses Equals to distinguish keys, so lookup for key with Value=2 returns "Two".
🧠 Conceptual
expertChoosing Data Structure for Frequent Lookups and Insertions
You need a data structure that supports fast lookups and insertions by key with minimal memory overhead. Which is the best choice?
Attempts:
2 left
💡 Hint
Consider average time complexity and memory usage for each structure.
✗ Incorrect
Hash maps provide average O(1) lookup and insertion, making them ideal for frequent operations with reasonable memory use.
