What is an Associative Array?

๐Ÿ’ก Concept Name

Associative Array โ€” A data structure that lets you store and retrieve values using custom keys instead of just numbers. Also known as a dictionary or map in most programming languages.

๐Ÿ“˜ Quick Intro

An associative array lets you find information not by position, but by a special key you choose (like a name or code). In C# and .NET, this powerful idea shows up as Dictionary<TKey, TValue>.

๐Ÿง  Analogy / Short Story

Picture your phoneโ€™s contact list. You never scroll by numberโ€”you type in the name (the key) and instantly get their number (the value). Associative arrays bring this convenience to programming!

๐Ÿ”ง Technical Explanation

  • ๐Ÿ”‘ Values are stored as key-value pairs, not just in order.
  • โšก Key lookups are super fastโ€”usually O(1) thanks to hashing.
  • ๐Ÿ›ก๏ธ Keys must be unique (no duplicates allowed), but values can repeat.
  • ๐ŸŒŽ Keys can be any hashable typeโ€”string, int, even custom objects.
  • ๐Ÿ› ๏ธ Backed by a hash table (or sometimes a balanced tree for sorted maps).

๐ŸŽฏ Purpose & Use Case

  • โœ… Lookup tables (country codes, product IDs, settings).
  • โœ… Caching results for fast reuse.
  • โœ… Counting or tallying (like word frequencies).
  • โœ… Storing configurations, user preferences, or feature toggles.

๐Ÿ’ป Real Code Example

// Creating and using a dictionary
var capitals = new Dictionary<string, string> {
    { "USA", "Washington, D.C." },
    { "UK", "London" },
    { "India", "New Delhi" }
};

Console.WriteLine(capitals["India"]); // Output: New Delhi

โ“ Interview Q&A

Q1: What is an associative array?
A: A structure where each value is linked to a unique key.

Q2: What is the .NET equivalent?
A: Dictionary<TKey, TValue> in C#.

Q3: Can keys repeat in a dictionary?
A: No, every key must be unique.

Q4: Can values repeat?
A: Yes, values can be anythingโ€”even duplicated.

Q5: What is the average time complexity for key lookup?
A: O(1), thanks to hash tables.

Q6: Is the order of keys preserved?
A: No, unless you use OrderedDictionary or SortedDictionary.

Q7: Can dictionaries grow or shrink as you add/remove entries?
A: Yes, they resize as needed.

Q8: What if you add a duplicate key?
A: It throws an exception at runtime.

Q9: How is a dictionary implemented in .NET?
A: With a hash table under the hood.

Q10: Are string keys case-sensitive in C#?
A: Yes, unless you use a custom comparer.

๐Ÿ“ MCQs

Q1. What does an associative array use for indexing?

  • Numbers
  • Keys
  • Booleans
  • Pointers

Q2. Which C# type represents an associative array?

  • List
  • Array
  • Dictionary
  • Queue

Q3. What is true about keys in associative arrays?

  • They can be duplicated
  • They must be unique
  • They must be integers
  • They must be lowercase

Q4. What is the average time complexity for a key lookup in Dictionary?

  • O(1)
  • O(n)
  • O(log n)
  • O(n log n)

Q5. Can values be duplicated in a Dictionary?

  • Yes
  • No
  • Only integers
  • Only strings

Q6. Which of the following is NOT an associative array?

  • Dictionary
  • Map
  • Hashtable
  • Stack

Q7. Which data structure allows fast access using a key?

  • Array
  • Stack
  • Associative Array
  • List

Q8. Which statement is correct about .NET Dictionary?

  • It is implemented using arrays
  • It uses trees
  • It is implemented using hash tables
  • It is unordered list

Q9. Can associative arrays grow dynamically?

  • No
  • Yes
  • Only up to 1000 items
  • Only during compile time

Q10. What happens if you insert duplicate keys?

  • Values are merged
  • Both keys are stored
  • Exception is thrown
  • Older value is kept

๐Ÿ’ก Bonus Insight

Try TryGetValue() to safely fetch a value without risking an exception if the key isnโ€™t found. For multi-threaded code, use ConcurrentDictionary for built-in thread safety.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: