Frontend School

Bigbasket Category Navigator: Interactive Tree View

Machine Coding
medium

Bigbasket Category Navigator: Interactive Tree View

Medium
React
Tree View
UI Components
Recursion
State Management

Overview

Bigbasket organizes its vast product catalog into a deep hierarchy of categories and subcategories. To enhance user experience and facilitate efficient navigation, we need an intuitive way for users to explore this structure. This problem challenges you to build an interactive Tree View component that allows users to browse Bigbasket's categories by expanding and collapsing nodes, providing a clear visual representation of the hierarchy.

What you need to do / Task

Your primary task is to develop a React component that renders a hierarchical list of categories as an interactive Tree View.

  1. Render Hierarchy: Display the provided mock category data, respecting its nested structure.
  2. Expand/Collapse Functionality: Implement the ability to expand and collapse parent categories. Clicking on a parent category's toggle or label should reveal/hide its direct children.
  3. Visual Indentation: Ensure that child categories are visually indented relative to their parent categories, clearly indicating their depth in the hierarchy.
  4. Sub-item Count: Next to each category name, display the total count of direct sub-items (either subcategories or individual products if the category is a leaf node). The mock data will provide productCount for leaf nodes and children array for parent nodes.
  5. Leaf Node Interaction: For categories that are leaf nodes (i.e., have no children), clicking their label should trigger a console.log displaying the category's name and ID.

UI/UX expectations

  • Toggle Indicator: Use a clear visual indicator (e.g., an arrow icon ▶ for collapsed, ▼ for expanded) next to expandable nodes. Leaf nodes should not have this indicator.
  • Indentation: Each level of depth should have a consistent, discernible indentation (e.g., using padding-left or margin-left).
  • Clickable Areas: The category name and its associated toggle (if any) should be clickable.
  • Basic Styling: Ensure readability with basic styling for text and spacing. No need for elaborate designs.

Acceptance criteria

  • The entire category tree, based on the mock data, renders correctly on initial load.
  • Clicking a parent category's toggle or label correctly expands its children if collapsed, and collapses them if expanded.
  • The expand/collapse indicator icon visually updates its state (e.g., rotates or changes shape) upon interaction.
  • Leaf nodes (categories without children) are rendered without an expand/collapse toggle.
  • Each category is indented according to its depth in the hierarchy.
  • The count of direct sub-items (children categories or products) is accurately displayed next to each category name.
  • Clicking on a leaf node's name logs its name and id to the console.

Constraints

Constraints & assumptions

  • You must use React for building the UI component.
  • The category data structure will be an array of objects, potentially nested, as provided in the mockCategories.
  • The maximum depth of the category hierarchy will be around 5-6 levels.
  • The total number of nodes in the tree will be manageable (e.g., up to 100-200 nodes), so performance optimization for extremely large trees is not a primary concern for this problem, but efficient rendering is expected.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed; build components from scratch using native HTML elements and React.
  • You can assume a modern browser environment.

Out of scope

  • Fetching data from an API (use the provided mock data).
  • Search, filter, or sort functionality for categories.
  • Drag-and-drop reordering of categories.
  • Persistence of expanded/collapsed states across page loads or component unmounts.
  • Complex accessibility features beyond standard semantic HTML and basic keyboard interaction.

What interviewers evaluate

  • Component Design & State Management: How well the problem is decomposed into reusable components and how state is managed effectively.
  • Recursive Rendering Logic: Correct implementation of recursive rendering to handle arbitrary tree depths.
  • UI/UX Implementation: Accuracy and responsiveness of expand/collapse functionality, indentation, and visual feedback.
  • Code Quality: Readability, maintainability, adherence to best practices, and effective use of TypeScript features (if applicable).

Loading editor...