LearnFrenzy
Sign in Take a free test

Callbacks · Exercise 4 of 4

Callbacks Callbacks Coding Problems

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Callback programs asked in interviews: retrying a failing task, running tasks in series and in parallel, progress callbacks, success/failure callbacks, timeouts, promisifying a callback API, custom sort comparators, a countdown with setInterval and a waterfall. Write each one yourself first, then open the solution – every solution was run on Node.js and the output shown is the real output.

Question 1 Hard

Write retry(task, attempts, cb) that runs an error-first async task, retries it on failure up to attempts times, and reports the final result or error. Test it with a task that fails twice and then succeeds.

Question page & discussion
Question 2 Medium

Write series(tasks, done) that runs callback-based tasks one at a time, in order, and calls done with all results (or the first error).

Question page & discussion
Question 3 Hard

Write parallel(tasks, done) that starts all callback tasks at once, stores each result at its task's index and calls done once when every task has finished (or once with the first error).

Question page & discussion
Question 4 Easy

Write download(sizeMB, onProgress, onDone) that simulates downloading in 25% chunks, calling onProgress for each chunk and onDone at the end.

Question page & discussion
Question 5 Easy

Write register(user, onSuccess, onFailure) that validates a name, an email and a password and calls exactly one of the two callbacks.

Question page & discussion
Question 6 Hard

Write withTimeout(task, ms) that returns a new task: it calls back with the task's result if it finishes in time, or with a timeout error otherwise – and never calls back twice.

Question page & discussion
Question 7 Medium

Given a callback-based getMarks(rollNo, cb), write a promisify helper and use it with then/catch to print the marks of two students, one of whom does not exist.

Question page & discussion
Question 8 Easy

Sort a list of products with three different comparator callbacks: by price, by rating (highest first) and by name, and print the top item for each.

Question page & discussion
Question 9 Easy

Write countdown(from, onTick, onDone) using setInterval that calls onTick with each remaining number and onDone at zero.

Question page & discussion
Question 10 Medium

Write waterfall(steps, done) where each step receives the previous step's result and an error-first callback; stop at the first error. Use it to process an order.

Question page & discussion
All exercises doneBack to CallbacksRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links