Retrieve All Descendant Categories
Blinkit's product catalog is extensive and organized into a hierarchical structure of categories. For example, "Beverages" might have "Juices" and "Soft Drinks" as subcategories, and "Juices" might further have "Fresh Juices" and "Packaged Juices". Maintaining and navigating this structure efficiently is crucial for inventory management, personalized recommendations, and user experience.
Your task is to implement a function that, given a starting category ID and a flat list of all categories, returns a list of all descendant category IDs. This includes direct subcategories, their subcategories, and so on, down to the deepest level.
Function Signature:
function getAllDescendantCategoryIds(categoryId: number, categories: Category[]): number[]
Input:
categoryId: A number representing the ID of the parent category for which we need to find all descendants.categories: An array of Category objects. Each Category object has the following structure:
interface Category {
id: number;
name: string; // A descriptive name, not used for logic
parentId: number | null; // The ID of the parent category, or null if it's a top-level category
}
Output:
number[] representing a list of unique IDs of all categories that are descendants (direct or indirect) of the given categoryId. The order of IDs in the output array does not matter.categories array can contain categories that are not descendants of the categoryId, or even categories that are ancestors. You should only return descendants.categoryId itself should NOT be included in the output list.categoryId does not exist or has no descendants, an empty array should be returned.parentId can be null for root categories.Examples
Example 1
Input:
categoryId = 1, categories = [{"id":1,"name":"Electronics","parentId":null},{"id":2,"name":"Mobiles","parentId":1},{"id":3,"name":"Smartphones","parentId":2},{"id":4,"name":"Laptops","parentId":1}]
Output:
[2,3,4]
Note:
Category 1 (Electronics) has direct children 2 (Mobiles) and 4 (Laptops). Category 2 (Mobiles) has direct child 3 (Smartphones). All are descendants of 1.
Example 2
Input:
categoryId = 3, categories = [{"id":1,"name":"Electronics","parentId":null},{"id":2,"name":"Mobiles","parentId":1},{"id":3,"name":"Smartphones","parentId":2},{"id":4,"name":"Laptops","parentId":1}]
Output:
[]
Note:
Category 3 (Smartphones) has no subcategories, so an empty array is returned.
Example 3
Input:
categoryId = 1, categories = [{"id":1,"name":"Groceries","parentId":null},{"id":2,"name":"Fruits","parentId":1},{"id":3,"name":"Apples","parentId":2},{"id":4,"name":"Vegetables","parentId":1},{"id":5,"name":"Dairy","parentId":null},{"id":6,"name":"Milk","parentId":5}]
Output:
[2,3,4]
Note:
Categories 5 (Dairy) and 6 (Milk) are not descendants of category 1 (Groceries) and are correctly excluded from the result.
Constraints
N) in the categories array will be between 0 and 100.categories array.categoryId will be a positive integer.categoryId not existing?Loading editor...