HomeInterview QuestionsGiven a nested dictionary, print all leaf elements…

Given a nested dictionary, print all leaf elements (values that are not dictionaries).

🟡 Medium Coding Junior level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

A leaf element is any value that is not a dictionary. You can collect them with a simple recursive function: iterate over each key, if the value is a dict, recurse; otherwise, append the value to a list. For example, with the dictionary {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}, 'f': {'g': 4}}, the function will return [1, 2, 3, 4]. The algorithm visits each node once, giving O(n) time complexity, and uses O(h) stack space, where h is the maximum depth of nesting.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500