Problem that doesn’t have a standard answer. The unstructured nature of SDIs, where they are asked to work on an open-ended design.So the short URL will be the counter and here is a code snippet assuming that your server is running properly.System Design Interviews: A step by step guideĪ lot of software engineers struggle with system design interviews (SDIs) primarily because of three Since the counter will be unique if we are storing the data in the same machine we can get it with no doubts that it will be duplicate. j1k2l3 represents the counter, starting with a random value.a1b2c3d4 represents the seconds since the Unix epoch,.a 3-byte counter (in your machine), starting with a random value.a 4-byte value representing the seconds since the Unix epoch,.Since we know the format that MongoDB uses to create a new ObjectId with 12 bytes. Now find your database-record with WHERE id = 19158 and do the redirect.Įxample implementations (provided by commenters) You just do a reverse lookup in your alphabet.Į9a 62 will be resolved to "4th, 61st, and 0th letter in the alphabet".Į9a 62 = 4×62 2 + 61×62 1 + 0×62 0 = 19158 10 How to resolve a shortened URL to the initial ID With 2 → c and 1 → b, you will receive cb 62 as the shortened URL. This is how your mapping (with an array for example) could look like: 0 → a Now map the indices 2 and 1 to your alphabet. This requires the use of integer division and modulo. Now you have to convert 125 10 to X 62 (base 62). Take an auto-generated, unique numerical key (the auto-incremented id of a MySQL table for example).įor this example, I will use 125 10 (125 with a base of 10).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |