---
# System prepended metadata

title: 'Problem 2: Category'

---

# Problem 2: Category

You are an engineer working in an e-commerce company. You are working on the API for the `category`. 

Given the raw records from the database, please implement a function to generate a data structure that represents the hierarchy of the `category`. 


```javascript=
function generateHierarchy(categories) {
    // your implementation
}
```

## Example:

Given the raw records of `category`: 

```javascript==
const category = [
        {
            id: 0,
            name: "root_category",
            display_priority: 1,
            parent_id: null,
        },
        {
            id: 1,
            name: "food",
            display_priority: 1,
            parent_id: 0,
        },
        {
            id: 2,
            name: "drink",
            display_priority: 2,
            parent_id: 0,
        },
        {
            id: 3,
            name: "milk",
            display_priority: 1,
            parent_id: 2,
        },
        {
            id: 4,
            name: "meat",
            display_priority: 2,
            parent_id: 1,
        },
        {
            id: 5,
            name: "beef",
            display_priority: 1,
            parent_id: 4,
        },
        {
            id: 6,
            name: "snack",
            display_priority: 1,
            parent_id: 1,
        },
        
    ];

```

The expected output is as follows.

```javascript=

const output = generateHierarchy(categories);

console.log(output); 

/*
[
    {
        id: 1,
        name: "food",
        display_priority: 1,
        children: [
            {
             id: 6
             name: "snack",
             display_priority: 1
             children: []
            },
            {
                id: 4
                name: "meat",
                display_priority: 2
                children: [
                    {
                     id: 5
                     name: "beef",
                     display_priority: 1
                     children: []
                    },
                ]
            },
        ]
    },
    {
        id: 1,
        name: "drink",
        display_priority: 2,
        children: [
            {
                id: 3
                name: "milk",
                display_priority: 1
                children: []
            }
        ]
    }
]
 
 * */
```

### Explanation:

The hierarchy of `category` is:

```graphviz
digraph graphname{
		root_category->food
        root_category->drink
        drink->milk
        food->snack
        food->meat
        meat->beef
	}
```


- The children categories would be listed and sorted by the `display_priority` in the parent category's children.

- The output ignores the `root_category` (as it is just the entry point of the category's hierarchy).

## Guidelines

- You can use `Typescript` or `Javascript` to finish it.
- No **external package** or **library** can be used.
- Feel free to write some comments to explain what you do.

## Submission

- Write all your code in a `p2.js` or `p2.ts` file