# Flatten a Dictionary — coding interview question

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/3050/flatten-a-dictionary
Source observed: 2026-11-05T00:28:07.443Z
Last checked: 2026-11-05T00:28:07.443+00:00
Indexed: 2026-11-07T00:47:13.034+00:00

---

# Flatten a Dictionary

We provide this **Medium** coding interview question in our **Data Structures & Algorithms** and **Coding** categories. It asks you to write a function named `flattenDictionary` that converts a nested dictionary into a flat dictionary.

## Problem

Given a dictionary `dict`, write `flattenDictionary` so it returns a flattened version of the dictionary. Values may be an integer, a string, or another dictionary.

If a key is empty, exclude that key from the output. In the example, the nested key `e` contains an empty key (`""`), so the empty key is omitted while `e` remains part of the flattened path.

When concatenating nested keys, place a dot (`.`) between each key. For example, the path formed from `Key2`, `c`, and `d` must be `Key2.c.d`.

## Example

```text
input:  dict = {
    "Key1" : "1",
    "Key2" : {
        "a" : "2",
        "b" : "3",
        "c" : {
            "d" : "3",
            "e" : {
                "" : "1"
            }
        }
    }
}

output: {
    "Key1" : "1",
    "Key2.a" : "2",
    "Key2.b" : "3",
    "Key2.c.d" : "3",
    "Key2.c.e" : "1"
}
```

## Practice interface

The question includes **Question**, **Solution**, and **Community** views, with hints and challenges available in the interface. The code editor provides **Editor**, **Share**, **Output**, **Test Results**, and **Console** panels. We invite you to [sign up to run code](https://www.aced.io/signup?src=lesson). Detailed company statistics for this question are available through [Upgrade](https://www.aced.io/upgrade?src=questions).

## Related question

- [Alien Dictionary](https://www.aced.io/questions/7311/define-alien-dictionary-problem)

## Explore Aced

We are Aced, formerly Exponent, and we provide interview preparation through courses, practice questions, interview loops, experiences, coaching, and related career services. Relevant question collections include [All Questions](https://www.aced.io/questions), [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms), and [Coding](https://www.aced.io/questions?type=coding).

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3050%2Fflatten-a-dictionary&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F3050%2Fflatten-a-dictionary). Opening a form does not execute a tool.