Sequence NODE_232
MediumDesign URL Shortener Core Logic
Node.js
Technical Specification
Implement the in-memory logic for a URL shortener: createShortUrl(longUrl) and getLongUrl(shortCode).
Input/Output Samples
Input:createShortUrl('https://example.com')
Output:abc123
Input:getLongUrl('abc123')
Output:https://example.com
Optimal Logic Path
const store = new Map();
function createShortUrl(longUrl) {
const code = Math.random().toString(36).slice(2, 8);
store.set(code, longUrl);
return code;
}
function getLongUrl(code) {
return store.get(code) || null;
}
module.exports = { createShortUrl, getLongUrl };Architectural Deep-Dive
We map random short codes to original URLs; in real apps we'd also validate and persist.