Design Pastebin
📋 What is Pastebin? It is a service where you paste a piece of text, such as a log, a config file or a code snippet, and get back a short link to share. Anyone with the link can read the text, unless you made it private. Many pastes are meant to be temporary, so they can expire after a few minutes or days.
Pastebin is a good easy problem because it has three small rules that each push the design in a direction: ids must be short and unique, pastes can expire, and visibility decides who may read a paste and whether it is listed. None of them is hard on its own. The test is whether you keep each rule in one place, and whether you can say what an expired paste costs you in memory.
Requirements
"Design a Pastebin. Users paste text and get a short id back. Pastes can expire, and they can be public or private."
Clarifying Questions
You: Who can read a paste?
Interviewer: There are three levels. A public paste is readable by anyone and shows up in a "recent pastes" list on the home page. An unlisted paste is readable by anyone who has the link, but it is never listed. A private paste is readable only by the user who created it.
You: Do users need an account?
Interviewer: No. Anonymous users can create public and unlisted pastes. A private paste needs an owner, otherwise nobody could ever read it, so we refuse it.
You: How does expiry work?
Interviewer: The user picks a lifetime in minutes when they create the paste, or zero for "never expires". Once the lifetime has passed, the paste is gone: reading it fails exactly as if it never existed.
You: Can pastes be edited or deleted?
Interviewer: No editing. The owner can delete their paste. Anonymous pastes have no owner, so they can only expire.
You: What do ids look like?
Interviewer: Short, since they go in a URL. Use a counter written in base 62, so the tests are predictable. We can talk about guessable ids later.
You: Should the service read the clock?
Interviewer: No, every call gets a timestamp in minutes.
The first answer is the one that shapes the code. "Unlisted" is not a weaker form of private: it is about listing, not about access. Keep those two questions apart and the visibility rule becomes two tiny checks.
💡 Interview tip: When a prompt says "public or private", ask whether there is a middle ground. Most real sharing products have one (unlisted links, "anyone with the link"), and the moment you name it, the interviewer sees you thinking about the product, not just the code.
Final Requirements
- A user (or an anonymous user, written
"") creates a paste with some text, a visibility and a lifetime in minutes (0means forever). The service returns a short unique id. - Empty text and negative lifetimes are refused. An anonymous user cannot create a private paste.
- Ids are a counter in base 62 (
1,2, ...,z,A, ...,Z,10, ...). A refused paste does not use an id. - Reading a paste returns its text. It fails if the id is unknown, the paste has expired or been deleted, or it is private and the reader is not the owner.
- A paste created at time
twith lifetimettlis readable whilenow < t + ttl. - The owner can delete a paste. Nobody else can, and anonymous pastes cannot be deleted.
- Anyone can list the
knewest public pastes that still exist, newest first.
Out of scope
- Editing, syntax highlighting, passwords, view counters.
- Storing very large pastes (we come back to this in Extensibility).
- User accounts and sign-in: a user is just an id string.