Giter Site home page Giter Site logo

productsorting's Introduction

ProductSorting

product sort by parent id.

Suppose you are tasked with syncing data from two different sources, a product database, and a storefront. The product database also contains the product taxonomy, i.e., product categories. Each category has a name, can have at most one parent category, and can have zero or more children. The product database provides three pieces of information about a category: the category’s id, it’s parent category’s id (if any), and the category’s name. The storefront, though, has one limitation: a child category cannot be inserted before its parent category has been created. Our job is to write a function that can take a JSON string of categories provided by the product database and sort them in the optimal insertion order for the storefront so that no category insertion will result in an integrity error. The taxonomy for categories can be arbitrarily deep. The return value should also be a JSON string.

Your function should take a JSON object representing the categories from the product database and provide as output a list of dictionaries sorted in the proper insertion order. There may be more than one optimal ordering of the categories, but you only have to provide an optimal ordering.

You can assume:

  • The input will always be solvable (there will be no missing parents)
  • The input will always be valid JSON in the format of the example below, with no additional data
  • There may be more than one root category (a category with no parents)

This is a formatted JSON sample input, with one sample child (with both a parent and a child), one child with no children, and sample parent (with no parent)

[
  {
    "name": "Accessories",
    "id": 1,
    "parent_id": 20,
  },
  {
    "name": "Watches",
    "id": 57,
    "parent_id": 1
  },
  {
    "name": "Men",
    "id": 20,
    "parent_id": null
  }
]

This is a formatted JSON sample solution for the input above:

[
  {
    "name": "Men",
    "id": 20,
    "parent_id": null
  },
  {
    "name": "Accessories",
    "id": 1,
    "parent_id": 20
  },
  {
    "name": "Watches",
    "id": 57,
    "parent_id": 1
  }
]

When submitting your solution, please provide it as a file containing the function or method which will perform the proper calculations, so we can run tests against it. Below is the general format of an acceptable submission in Javascript:

module.exports = function sortCategoriesForInsert (inputJson) { // Your code happens... /// ... which calculates properJsonOutput return properJsonOutput } Final notes on expected submissions:

  • Please use standard libraries where possible and limit the business logic to a one file submission.
  • The order of key-value pairs within the JSON output does NOT matter.
  • The whitespace of output JSON does NOT matter.
  • Your solution should take into account that there may be tens of thousands of categories.

productsorting's People

Contributors

ketulsuthar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.