We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Recursion Practice

In Doc2Doc, we might have documents nested inside other documents, forming a kind of tree. You know how crazy .docx files can get...

Anyway, we want to find out how deeply nested a given document is.

Assignment

Complete the count_nested_levels function. It takes a dictionary of nested documents, the target document ID, and the current level of the document.

Example

In this dictionary, the document with ID 3 is nested 2 levels deep. Document 2 is nested 1 level deep.

nested_documents: dict[int, dict] = {1: {3: {}}, 2: {}}

Tips

The -1 return value is the "not found" signal. If a recursive call returns -1, the target isn't in that branch.