Problem: Sort Reports with Pluggable Algorithms

Easy
15 min
Build a report sorter that can switch between multiple sorting algorithms without modifying the main logic.

Problem statement

You’re building a report generator for a logistics dashboard. The dataset must be sortable by name, date, or score, depending on user preference.

Sorting logic often ends up buried in long if blocks that grow with each new sorting rule. This makes the code harder to extend and maintain over time. Design the system using the Strategy Pattern so new sorting behaviors can be added safely without touching the main flow.

Goal

Implement both parts of the Strategy Pattern:

  1. Strategies: NameSortStrategyDateSortStrategy, and ScoreSortStrategy classes—each must define .sort(data), implementing a distinct sorting rule.

  2. Context: ReportSorter class that receives a sorting strategy in its constructor, delegates to it in .sort(), and allows swapping strategies via .setStrategy().

Constraints

  • ReportSorter must not use if or switch to pick the strategy.

  • Strategies must not depend on each other.

  • The code should work when new strategies are added without modifying ReportSorter.

Sample output

The examples below illustrate what the output should look like:

const reports = [
{ name: 'Zara', date: '2023-11-01', score: 92 },
{ name: 'Alice', date: '2023-10-15', score: 78 },
{ name: 'Bob', date: '2023-11-03', score: 85 }
];
// Instantiate sorter with different strategies
const sorter = new ReportSorter(new NameSortStrategy());
console.log('By name:', sorter.sort(reports));
/* Expected output:
By name: [ { name: 'Alice', ... }, { name: 'Bob', ... }, { name: 'Zara', ... } ]
*/
sorter.setStrategy(new DateSortStrategy());
console.log('By date:', sorter.sort(reports));
/* Expected output:
By date: [ { name: 'Alice', ... }, { name: 'Zara', ... }, { name: 'Bob', ... } ]
*/
sorter.setStrategy(new ScoreSortStrategy());
console.log('By score:', sorter.sort(reports));
/* Expected output:
By score: [ { name: 'Zara', ... }, { name: 'Bob', ... }, { name: 'Alice', ... } ]
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Sort Reports with Pluggable Algorithms

Easy
15 min
Build a report sorter that can switch between multiple sorting algorithms without modifying the main logic.

Problem statement

You’re building a report generator for a logistics dashboard. The dataset must be sortable by name, date, or score, depending on user preference.

Sorting logic often ends up buried in long if blocks that grow with each new sorting rule. This makes the code harder to extend and maintain over time. Design the system using the Strategy Pattern so new sorting behaviors can be added safely without touching the main flow.

Goal

Implement both parts of the Strategy Pattern:

  1. Strategies: NameSortStrategyDateSortStrategy, and ScoreSortStrategy classes—each must define .sort(data), implementing a distinct sorting rule.

  2. Context: ReportSorter class that receives a sorting strategy in its constructor, delegates to it in .sort(), and allows swapping strategies via .setStrategy().

Constraints

  • ReportSorter must not use if or switch to pick the strategy.

  • Strategies must not depend on each other.

  • The code should work when new strategies are added without modifying ReportSorter.

Sample output

The examples below illustrate what the output should look like:

const reports = [
{ name: 'Zara', date: '2023-11-01', score: 92 },
{ name: 'Alice', date: '2023-10-15', score: 78 },
{ name: 'Bob', date: '2023-11-03', score: 85 }
];
// Instantiate sorter with different strategies
const sorter = new ReportSorter(new NameSortStrategy());
console.log('By name:', sorter.sort(reports));
/* Expected output:
By name: [ { name: 'Alice', ... }, { name: 'Bob', ... }, { name: 'Zara', ... } ]
*/
sorter.setStrategy(new DateSortStrategy());
console.log('By date:', sorter.sort(reports));
/* Expected output:
By date: [ { name: 'Alice', ... }, { name: 'Zara', ... }, { name: 'Bob', ... } ]
*/
sorter.setStrategy(new ScoreSortStrategy());
console.log('By score:', sorter.sort(reports));
/* Expected output:
By score: [ { name: 'Zara', ... }, { name: 'Bob', ... }, { name: 'Alice', ... } ]
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// --- Strategies (implement these classes) ---
class NameSortStrategy {}
class DateSortStrategy {}
class ScoreSortStrategy {}
// --- Context (implement this class too) ---
class ReportSorter {
constructor(strategy) {}
setStrategy(strategy) {}
sort(data) {}
}
// Example usage
const reports = [
{ name: 'Zara', date: '2023-11-01', score: 92 },
{ name: 'Alice', date: '2023-10-15', score: 78 },
{ name: 'Bob', date: '2023-11-03', score: 85 }
];
// Instantiate sorter with different strategies
const sorter = new ReportSorter(new NameSortStrategy());
console.log('By name:', sorter.sort(reports));
/* Expected output:
By name: [ { name: 'Alice', ... }, { name: 'Bob', ... }, { name: 'Zara', ... } ]
*/
sorter.setStrategy(new DateSortStrategy());
console.log('By date:', sorter.sort(reports));
/* Expected output:
By date: [ { name: 'Alice', ... }, { name: 'Zara', ... }, { name: 'Bob', ... } ]
*/
sorter.setStrategy(new ScoreSortStrategy());
console.log('By score:', sorter.sort(reports));
/* Expected output:
By score: [ { name: 'Zara', ... }, { name: 'Bob', ... }, { name: 'Alice', ... } ]
*/