interview-questions
dictionary-order
sorting-algorithms
programming-challenges
coding-interview

Question from Interview, Retrieve alphabetic order from dictionary

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A dictionary gives fast lookup by key, not automatic alphabetical ordering. So if an interview asks you to retrieve dictionary entries in alphabetic order, the core answer is usually "extract what you want and sort it". The details depend on whether you are sorting keys, values, or full key-value pairs.

Clarify What "Alphabetic Order" Means

Interview questions often leave out the most important detail: what exactly should be ordered.

Possible interpretations:

  • sort by keys alphabetically
  • sort by values alphabetically
  • output values in the order of sorted keys
  • sort case-sensitively or case-insensitively

A strong interview answer starts by clarifying that requirement instead of coding immediately.

Sorting by Keys in Python

If the goal is alphabetic order by key, use sorted on the dictionary or on dict.items().

python
1data = {
2    "banana": 3,
3    "apple": 5,
4    "cherry": 1,
5}
6
7for key in sorted(data):
8    print(key, data[key])

This produces output ordered by keys. Time complexity is O(n log n) because sorting dominates.

Sorting Full Items by Key

If you want a sorted list of pairs, sort data.items().

python
1data = {
2    "banana": 3,
3    "apple": 5,
4    "cherry": 1,
5}
6
7items = sorted(data.items(), key=lambda item: item[0])
8print(items)

This is often the cleanest representation for later processing because you already have both the key and value together.

Sorting by Value Instead

Sometimes the question is really about alphabetical order of dictionary values.

python
1data = {
2    "id1": "Charlie",
3    "id2": "Alice",
4    "id3": "Bob",
5}
6
7items = sorted(data.items(), key=lambda item: item[1])
8print(items)

Now the sort key is the value rather than the dictionary key.

Case-Insensitive Sorting

Alphabetic order can change when uppercase and lowercase letters mix. If the requirement is case-insensitive, normalize in the sort key.

python
1data = {
2    "Banana": 1,
3    "apple": 2,
4    "Cherry": 3,
5}
6
7for key in sorted(data, key=str.lower):
8    print(key, data[key])

This is a good interview detail to mention because it shows you are thinking beyond the happy path.

Dictionary Order Versus Sorted Order

Modern Python dictionaries preserve insertion order, but that is not the same as automatic alphabetical ordering.

python
1data = {}
2data["banana"] = 3
3data["apple"] = 5
4data["cherry"] = 1
5
6print(list(data.keys()))

The keys come back in insertion order, not sorted order. If you want alphabetical retrieval, you still need an explicit sort step.

Returning a New Ordered Mapping

If the caller wants a dictionary-like object in sorted order, create a new dictionary from the sorted items.

python
1data = {
2    "banana": 3,
3    "apple": 5,
4    "cherry": 1,
5}
6
7sorted_dict = dict(sorted(data.items()))
8print(sorted_dict)

This preserves sorted insertion order in modern Python, though you should still remember that the sorting happened at construction time, not automatically afterward.

Interview-Level Complexity Discussion

The lookup complexity of a dictionary is usually near O(1), but once sorting is required the problem becomes O(n log n). That is the right time complexity answer for the retrieval-in-alphabetic-order version because every item must participate in the ordering step.

A stronger interview answer also notes that if sorted access is required repeatedly, you might choose a different data structure or maintain a separate sorted key list instead of re-sorting on every query.

Common Pitfalls

The most common mistake is saying that dictionaries are already ordered and skipping the sorting step. Insertion order is not alphabetical order. Another is failing to clarify whether the interviewer means sorting by key or by value. Teams also forget about case-sensitivity and produce output that is technically sorted but not in the user-expected order. Finally, building code around repeated sorting can be inefficient if the requirement is frequent ordered traversal rather than one-off output.

Summary

  • Dictionaries provide fast lookup, not automatic alphabetical ordering.
  • Use sorted(data) or sorted(data.items()) when ordering by key.
  • Use a custom key function when ordering by value or case-insensitively.
  • Sorting changes the complexity to O(n log n).
  • In interviews, clarify what should be ordered before writing the solution.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.