Build a Local URL Shortener

Quick Overview

This question evaluates a candidate's ability to design and implement a local URL shortener, exercising competencies in URL validation, hash-based encoding and collision handling, in-memory mapping with JSON persistence, deduplication, edge-case handling for long inputs, and comprehensive test coverage.

Build a Local URL Shortener

Company: Shopify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Implement a local URL shortener that runs on a single machine. Design a small library or service with two core operations: - `shorten(long_url) -> short_code` or `short_url` - `resolve(short_code) -> original_url` Requirements: 1. Validate that the input is a well-formed URL and reject invalid input. 2. Generate a short code using a hash-based or similar encoding strategy. 3. Handle hash collisions correctly so that no original URL mapping is lost. 4. Store the mapping between short codes and original URLs in memory using a hash map. 5. Persist the in-memory mapping to a JSON file, and reload that file when the program starts again. 6. If the same original URL is shortened multiple times, return the same shortened result instead of creating duplicates. 7. Handle edge cases such as extremely long URLs. 8. Write tests for invalid input, collision handling, duplicate requests, and long URLs, with strong line and branch coverage. 9. Be prepared to discuss how memory usage grows with the number of stored URLs and what changes you would make if the data no longer fits comfortably on one machine. Assume this is a local-only implementation. Do not introduce external systems such as Redis or a database unless you can justify why they are necessary.

Quick Answer: This question evaluates a candidate's ability to design and implement a local URL shortener, exercising competencies in URL validation, hash-based encoding and collision handling, in-memory mapping with JSON persistence, deduplication, edge-case handling for long inputs, and comprehensive test coverage.

|Home/Coding & Algorithms/Shopify
Shopify logo
Shopify
Jan 24, 2026, 12:00 AM
easySoftware EngineerTechnical ScreenCoding & Algorithms
59
0

Implement a local URL shortener that runs on a single machine.

Design a small library or service with two core operations:

  • shorten(long_url) -> short_code or short_url
  • resolve(short_code) -> original_url

Requirements:

  1. Validate that the input is a well-formed URL and reject invalid input.
  2. Generate a short code using a hash-based or similar encoding strategy.
  3. Handle hash collisions correctly so that no original URL mapping is lost.
  4. Store the mapping between short codes and original URLs in memory using a hash map.
  5. Persist the in-memory mapping to a JSON file, and reload that file when the program starts again.
  6. If the same original URL is shortened multiple times, return the same shortened result instead of creating duplicates.
  7. Handle edge cases such as extremely long URLs.
  8. Write tests for invalid input, collision handling, duplicate requests, and long URLs, with strong line and branch coverage.
  9. Be prepared to discuss how memory usage grows with the number of stored URLs and what changes you would make if the data no longer fits comfortably on one machine.

Assume this is a local-only implementation. Do not introduce external systems such as Redis or a database unless you can justify why they are necessary.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...