AlgoMaster Logo
AlgoMasterSimplify an Avatar Storemedium

Simplify an Avatar Store

medium

Design, and then simplify, an AvatarStore for a profile service.

The service has one concrete workflow:

  • accept files whose names end in .jpg or .png, ignoring case;
  • reject files larger than 2048 KB; and
  • store accepted files under avatars/<fileName> on the local filesystem.

Actual file I/O is outside this exercise. Returning and recording the destination path is enough.

The starter code predicts a much larger media platform. It has handler and storage-provider abstractions, accepts WebP files, and routes larger images to a cloud backend. None of that was requested. Refactor it to the design the product needs now:

  • AvatarStore() creates an empty store.
  • String upload(String fileName, int sizeKb) checks file type before size and returns:
    • "INVALID_TYPE" for any extension other than .jpg or .png;
    • "TOO_LARGE" when a supported file is larger than 2048 KB; or
    • "STORED: avatars/<fileName>" after a valid upload.
  • int storedCount() returns the number of successful uploads.
  • String lastPath() returns the most recently stored path, or "" before the first successful upload.

Failed uploads do not change the count or the last path.

Example 1:

Input:

Output:

Explanation: Both files are supported and below the limit. The larger one still goes to the same local directory.

Example 2:

Input:

Output:

Explanation: WebP and cloud storage were guesses in the legacy design, not supported product features.

Constraints

  • 1 <= fileName.length <= 100
  • 0 <= sizeKb <= 10^6
  • fileName contains only ASCII characters and has no path separators.
  • At most 100 calls will be made across all methods.

Starter Code

The starter compiles but builds a plugin architecture around one local workflow. It also accepts an unsupported type and sends some files to an unrequested cloud backend.

How the design is graded

needs 7/10 to pass
  • Concrete current workflow

    Full marks when AvatarStore directly validates JPEG/PNG files and creates a local avatars path. Lose points heavily for WebP, video, cloud routing or other media and storage features not requested.

  • No premature plugin system

    Full marks for one focused class and, at most, small private helpers. Lose points for retaining media-handler or storage-provider interfaces, registries, factories, adapters or backend selection logic.

  • Validation and state

    Full marks when type is checked before size, only successful uploads increment the count, lastPath changes only on success, and exact statuses are returned. Lose points for accepting .jpeg or .webp, routing large files to cloud, or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new AvatarStore()null
upload("face.png", 500)"STORED: avatars/face.png"
upload("team.JPG", 1500)"STORED: avatars/team.JPG"
storedCount()2
lastPath()"avatars/team.JPG"

Both supported image types use the same local directory, regardless of file size within the limit.

Run checks these cases. Submit also runs a larger hidden set.