Practice problem · Shopify · Medium

URL Shortener Service

A URL shortener turns a long link into a short code and gives the long link back when someone follows the code. 3 stages in Python with an AI assistant. Pro.

Stage 1 of 3: Shorten and resolve

Background. A URL shortener turns a long link into a short code and gives the long link back when someone follows the code.

Implement Shortener in shortener.py.

  • shorten(url) returns the code for url: 7 characters, each from ALPHABET (the digits, then a–z, then A–Z). Different URLs get different codes. Shortening the same URL again returns the code it already has.
  • resolve(code) returns the URL behind code, or None when no URL has that code.
  • A URL is accepted when its scheme is http or https in any letter case (HTTPS:// is fine), it has a host, it contains no whitespace and no control characters, and it is at most MAX_URL_LENGTH (2048) characters long. Anything else raises InvalidURL and is not stored.
  • URLs are kept exactly as given, never normalised: https://e.com/a, https://e.com/a/ and https://e.com/A are three different URLs, and resolve returns the string that was shortened.

Example

s = Shortener()
code = s.shorten("https://example.com/articles/launch")
len(code)                                       # 7
s.resolve(code)                                 # "https://example.com/articles/launch"
s.shorten("https://example.com/articles/launch") == code   # True
s.resolve("zzzzzzz")                            # None
s.shorten("example.com/articles/launch")        # raises InvalidURL: no scheme

Done when Submit goes green.

Constraints

  • Python 3.12+ standard library only
  • Time limit: 10 seconds per test file
  • Editable: shortener.py
  • Read-only: all tests/test_*.py files

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Codes from a hash. The code comes from the link itself, and two links can want the same one.
  2. Stage 3: Survive a restart. The mapping goes to a file and comes back, and what came back has to count.

The files you get

  • Yours to edit: shortener.py.
  • Read-only: tests/__init__.py.
  • Tests you can read and run: tests/test_part1_shorten.py. Hidden tests run on Submit.

The round: an agent in the editor

The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.

Frequently asked, briefly answered

Is URL Shortener Service free?

It is a Pro problem. Free plan includes one complete problem, plus the first stage of several more, so you can see how a round works before you pay.

Which company asks something like this?

Shopify. It is modelled on Shopify's round: Open-ended — candidate's own IDE + AI tool of choice over screen share.

Do I bring my own AI tool?

No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, an answer-only assistant that can't write code, or no assistant at all.

What does the interviewer ask?

2 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the Pro plan starts at the first stage of this problem.

Start this problem → Sign in

More like it: every practice problem, how Shopify runs its AI round, the AI coding interview guide.