C# Dictionary Scenario-Based Coding Questions (M1 Mock Test)
Practice 20 interview-style scenarios focused on Dictionary<TKey, TValue>. Each question includes a starter boilerplate to help associates begin quickly.
-
1) Inventory Lookup - Product Stock Finder
Create a dictionary for product code and stock count. Read a product code and print stock if present; otherwise print "Not Found".
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> inventory = new Dictionary<string, int> { { "P101", 25 }, { "P102", 0 }, { "P103", 14 } }; string inputCode = Console.ReadLine(); // TODO: Implement lookup and print result } } -
2) Student Marks - Update Existing Key
Store student names and marks. If a student exists, update mark; otherwise add new student.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> marks = new Dictionary<string, int> { { "Asha", 78 }, { "Bala", 82 } }; string student = Console.ReadLine(); int newMark = int.Parse(Console.ReadLine()); // TODO: Add or update mark } } -
3) Attendance Counter - Frequency Using Dictionary
Given an array of employee IDs, count attendance frequency of each employee.
using System; using System.Collections.Generic; class Program { static void Main() { int[] employeeIds = { 1001, 1002, 1001, 1003, 1002, 1001 }; Dictionary<int, int> attendance = new Dictionary<int, int>(); // TODO: Build frequency map and print } } -
4) Login Attempt Tracker - Increment Value
Track failed login attempts per user. Increment attempts whenever a username is received.
using System; using System.Collections.Generic; class Program { static void Main() { string[] attempts = { "raj", "kavi", "raj", "raj", "kavi" }; Dictionary<string, int> failedAttempts = new Dictionary<string, int>(); // TODO: Count attempts and print users with attempts >= 3 } } -
5) Country Code Resolver - Safe Access
Store country dialing codes and fetch a country code using
TryGetValue.using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, string> dialCodes = new Dictionary<string, string> { { "India", "+91" }, { "USA", "+1" }, { "Japan", "+81" } }; string country = Console.ReadLine(); // TODO: Use TryGetValue to print code or "Unavailable" } } -
6) Course Enrollment - Remove Key
Maintain course name and enrolled count. Remove a cancelled course from dictionary.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> courses = new Dictionary<string, int> { { "CSharp", 30 }, { "SQL", 28 }, { "Azure", 15 } }; string cancelledCourse = Console.ReadLine(); // TODO: Remove key if available and print remaining courses } } -
7) Price Catalog - Check Key Existence
Given an item name, validate whether it exists in catalog before billing.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, decimal> catalog = new Dictionary<string, decimal> { { "Pen", 12.5m }, { "Book", 90m }, { "Bag", 450m } }; string item = Console.ReadLine(); // TODO: Print item price if exists; else print "Invalid Item" } } -
8) City Temperature Board - Iterate Keys and Values
Print each city with temperature and find the hottest city.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> temperature = new Dictionary<string, int> { { "Chennai", 38 }, { "Delhi", 41 }, { "Bengaluru", 29 } }; // TODO: Iterate and find max temperature city } } -
9) Character Counter - String Analytics
Given input string, count occurrences of each character using dictionary.
using System; using System.Collections.Generic; class Program { static void Main() { string text = Console.ReadLine(); Dictionary<char, int> charCount = new Dictionary<char, int>(); // TODO: Build frequency and display sorted output } } -
10) Employee Directory - Merge Dictionaries
Merge branch1 and branch2 employee dictionaries. If duplicate key appears, prefer branch2 value.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<int, string> branch1 = new Dictionary<int, string> { { 101, "Anu" }, { 102, "Dev" } }; Dictionary<int, string> branch2 = new Dictionary<int, string> { { 102, "Devika" }, { 103, "Rahul" } }; // TODO: Merge dictionaries and print final result } } -
11) Word Dictionary - Case-Insensitive Keys
Build a dictionary that treats "apple" and "Apple" as same key.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, string> meanings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // TODO: Add words and demonstrate case-insensitive retrieval } } -
12) Ticket Priority Queue Snapshot - Group Counts
From ticket priorities array (High/Medium/Low), produce a dictionary count per priority.
using System; using System.Collections.Generic; class Program { static void Main() { string[] priorities = { "High", "Low", "Medium", "High", "High", "Low" }; Dictionary<string, int> priorityCount = new Dictionary<string, int>(); // TODO: Count each priority bucket } } -
13) Department Budget - Sum of Values
Store budgets for departments and print total budget utilization.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, double> budget = new Dictionary<string, double> { { "IT", 12.5 }, { "HR", 6.2 }, { "Finance", 9.1 } }; // TODO: Calculate and print total budget } } -
14) Last Seen Activity - DateTime Value Handling
Track last login time for users and print users inactive for more than 30 days.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, DateTime> lastLogin = new Dictionary<string, DateTime> { { "arun", DateTime.Today.AddDays(-10) }, { "meena", DateTime.Today.AddDays(-45) }, { "salim", DateTime.Today.AddDays(-60) } }; // TODO: Filter and print inactive users } } -
15) API Response Cache - Add If Missing
Use dictionary as cache. Add response only when URL key is not present.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, string> cache = new Dictionary<string, string>(); string url = Console.ReadLine(); string response = Console.ReadLine(); // TODO: Cache value only for new URL and print cache size } } -
16) Sales By Region - Nested Dictionary Intro
Track region-wise product sales count using nested dictionary.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, Dictionary<string, int>> sales = new Dictionary<string, Dictionary<string, int>>(); // TODO: Add data for region/product and print a summary report } } -
17) Duplicate Detector - First Repeated Number
Given integer array, use dictionary to identify first repeating value.
using System; using System.Collections.Generic; class Program { static void Main() { int[] nums = { 5, 1, 4, 2, 4, 3, 5 }; Dictionary<int, int> seen = new Dictionary<int, int>(); // TODO: Print first repeated number } } -
18) Transport Route Planner - Invert Dictionary
Convert city-to-route dictionary into route-to-list-of-cities dictionary.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, string> cityRoute = new Dictionary<string, string> { { "Pune", "R1" }, { "Mumbai", "R2" }, { "Nashik", "R1" } }; // TODO: Invert and print route-wise cities } } -
19) Exam Result Board - Topper Finder
From student marks dictionary, identify topper name(s) with highest mark.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, int> scores = new Dictionary<string, int> { { "Kiran", 92 }, { "Latha", 96 }, { "Nitin", 96 }, { "Pooja", 88 } }; // TODO: Find max score and print all toppers } } -
20) Mini Phonebook - CRUD Menu Scenario
Build a menu-driven phonebook using dictionary with Add, Search, Update, Delete operations.
using System; using System.Collections.Generic; class Program { static void Main() { Dictionary<string, string> phoneBook = new Dictionary<string, string>(); bool running = true; while (running) { Console.WriteLine("1.Add 2.Search 3.Update 4.Delete 5.Exit"); int choice = int.Parse(Console.ReadLine()); // TODO: Implement menu operations using dictionary } } }