Number Container System — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

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

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

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

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

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.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.