Track & Visualize

Number Container System

Choose Your Learning Path

How would you like to learn today? Visualize algorithms in real time, explore them step by step through reading, or challenge yourself with a test.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.

📖 Passive Learning

Read comprehensive problem explanation, reference links, and explore the code at your own pace.

🎯 Mandatory Challenge

Drag and arrange the algorithm steps in the correct execution order.

Understanding Number Container System
Detailed explanation and reference materials
Problem Overview

Number Container System

Design a system that stores numbers in "containers" (indexed slots) and supports two main operations:

1.change(index, number):
Set the container at the specified index to the given number. If a container already has a number, replace it with the new number.

  1. find(number):
    Return the smallest index that currently contains the specified number. If no container holds that number, return -1.

How It Works

  • Initialization:
    When the system is created, all containers are empty.
  • Updating a Container:
    When you call change(index, number), the system stores number at that index. If the container already held a number, it is replaced.
  • Searching for a Number:
    When you call find(number), the system searches for all containers containing that number and returns the smallest index among them. If none is found, it returns -1.

Example Test Cases

Example 1

Input:

["NumberContainers", "change", "change", "find", "change", "find"] [[], [2, 10], [5, 10], [10], [2, 20], [10]]

Expected Output:

[null, null, null, 2, null, 5]

Step-by-Step Explanation:

  1. Initialization:

    • Create a new NumberContainers instance.
  2. Operation: change(2, 10)

    • Set the container at index 2 to 10.
  3. Operation: change(5, 10)

    • Set the container at index 5 to 10.
  4. Operation: find(10)

    • Check all containers for the number 10.
    • Containers 2 and 5 contain 10.
    • The smallest index among these is 2.
    • Output: 2
  5. Operation: change(2, 20)

    • Update the container at index 2 to 20.
    • Now, container 2 no longer holds 10.
  6. Operation: find(10)

    • Check all containers for the number 10.
    • Only container 5 now holds 10.
    • Output: 5

Alternative Example 2

Input:

["NumberContainers", "change", "find", "change", "change", "find", "change", "find", "find"] [[], [3, 15], [15], [7, 15], [3, 20], [15], [7, 30], [15], [20]]

Expected Output:

[null, null, 3, null, null, 7, null, -1, 3]

Step-by-Step Explanation:

  1. Initialization:

    • Create a new NumberContainers instance.
  2. Operation: change(3, 15)

    • Set the container at index 3 to 15.
  3. Operation: find(15)

    • Check all containers for the number 15.
    • Container 3 contains 15.
    • Output: 3
  4. Operation: change(7, 15)

    • Set the container at index 7 to 15.
    • Now, containers 3 and 7 both have 15.
  5. Operation: change(3, 20)

    • Update the container at index 3 to 20.
    • Container 3 no longer holds 15; only container 7 holds 15.
  6. Operation: find(15)

    • Check all containers for the number 15.
    • Only container 7 contains 15.
    • Output: 7
  7. Operation: change(7, 30)

    • Update the container at index 7 to 30.
    • Container 7 no longer holds 15.
  8. Operation: find(15)

    • Check all containers for the number 15.
    • No container holds 15.
    • Output: -1
  9. Operation: find(20)

    • Check all containers for the number 20.
    • Container 3 holds 20.
    • Output: 3

Constraints

  • 1 <= index, number <= 10^9
  • There will be at most 10^5 calls combined to the change and find operations.

- Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
java
Reference Link
https://leetcode.com/problems/design-a-number-container-system/description
Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.

Loading component...

Java
Output:
Understood Algorithm , Test Me now 🎮
JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.

🧠 Logic Puzzle
Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

© 2026 | Privacy Policy | About