or run

npx @tessl/cli init
Log in

Version

Files

docs

index.md
tile.json

task.mdevals/scenario-2/

Product Price Comparator

Build a product comparison tool that efficiently maintains a sorted catalog and helps find the correct insertion position for new products based on their prices.

Requirements

You need to implement a product catalog system that:

  1. Maintains a sorted product catalog - Products are sorted by price in ascending order
  2. Finds insertion positions efficiently - When adding a new product, determine the correct index where it should be inserted to maintain sort order
  3. Supports price comparison based on product objects - Products are objects with various properties, and insertion position should be determined by the price property

Input Format

Products are represented as objects with the following structure:

{
  name: string,
  category: string,
  price: number
}

Output Requirements

Implement a function findInsertionIndex(catalog, newProduct) that:

  • Takes a sorted array of product objects (catalog) sorted by price in ascending order
  • Takes a new product object (newProduct)
  • Returns the index at which the new product should be inserted to maintain price order

@generates

API

/**
 * Finds the insertion index for a new product in a sorted catalog
 * @param {Array<Object>} catalog - Array of products sorted by price
 * @param {Object} newProduct - The product to insert
 * @returns {number} The index where the product should be inserted
 */
function findInsertionIndex(catalog, newProduct) {
  // Implementation here
}

module.exports = { findInsertionIndex };

Test Cases

  • Given an empty catalog [] and a product with price 50, returns index 0 @test
  • Given a catalog with products priced [10, 20, 30, 40] and a new product with price 25, returns index 2 @test
  • Given a catalog with products priced [10, 20, 30] and a new product with price 5, returns index 0 @test
  • Given a catalog with products priced [10, 20, 30] and a new product with price 35, returns index 3 @test
  • Given a catalog with products priced [15, 25, 25, 40] and a new product with price 25, returns index 1 (first position where 25 can be inserted) @test

Dependencies { .dependencies }

lodash { .dependency }

Provides utility functions for working with arrays and objects.