When practicing solving algorithm problems, we often see questions that make us wonder if we would ever encounter similar situations in the real world (e.g. spiral traversal of a matrix).
This time, however, I came across an interesting algorithm challenge that makes practical sense to me.
Here’s the task:
You are given a list of tasks, some of which depend on others.
Write a function
tasksInOrder
that takes a subset of those tasks and returns an ordered list of all the tasks to run.
To illustrate:
const tasks = [
{
task: "make a sandwich",
depends: [ "buy groceries" ]
},
{
task: "buy groceries",
depends: [ "go to the store" ]
},
{
task: "go to the store",
depends: []
}
]
// tasksInOrder(tasks, ["make a sandwich"])
// -> [ 'go to the store', 'buy groceries', 'make a sandwich' ]
// tasksInOrder(tasks, ["buy groceries", "make a sandwich"])
// -> [ 'go to the store', 'buy groceries', 'make a sandwich' ]
We all make to-do lists in our daily lives, so I was glad to finally see a function that we can actually put to good, practical use.
#javascript #coding #programming #algorithms #web-development