🔗
URL Shortener
Week 11
You are working for a government agency - your boss has tasked you with creating an in-house link-shortening website (like bit.ly) to allow webpage links be shared both internally and externally, with a short, but official-looking URL
It is agreed that using 3-character base-36 alphanumeric (numbers & uppercase letters - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") will strike a good balance between keeping the IDs short and unambiguous, while providing a large number of possible codes. In future, if all 3-character IDs are exhausted, the organisation will simply start using 4 character IDs
In the meeting, a few different solutions for generating the IDs are suggested by your colleagues, but you note a few disadvantages:
- Generate the IDs sequentially - i.e. "000", "001", "002" etc. - predictable, easy for third-parties to scrape & know how many links etc
- Generate the IDs randomly until you find one that hasn't been used before - inefficient when nearly all IDs are in use, since you have to keep generating until you find an unused one...or you increase the number of characters, but then don't use all the 3 character IDs
- Generate all IDs, shuffle them, save them to a file/database and choose the next available ID each time you need one - not suitable if in future you need more characters/higher base, as huge file needs to be pre-generated which wastes server storage (e.g. 64 ^ 13 for YouTube video IDs)
Instead, you suggest a clever solution that can generate all 3-digit codes efficiently, without having to pre-compute and being less obvious for a third party to try and guess the pattern:
- Instead of incrementing by 1, you will increment by something much less obvious - our sequence will start from 0 and increment by 2611, since we're in 2026 and this is challenge 11
- This number will be converted into its base-36 ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") equivalent
- Once you have output all ID codes in the sequence when starting from 0, you will then go back and start from 1
Below shows the start of the program output:
Note how we start from 0, increment by 2611 (0, 2611, 5222, 7833 etc) - when we exhaust the sequence starting from 0, we then go back and start from 1, which corresponds to the code "001". When we have exhausted this sub-sequence, we will then go back and start from 2, then 3, then 4...until we have output all the valid base-36 3-character ID codes
Paste all possible IDs along with the number they are generated from in the format specified:
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 | You will want a function (or inline code) to convert a number to its base 36 representation - since we are outputting 3 character codes, we will need the positional values (36 ^ 2 = 1296), (36 ^ 1 = 36) and (36 ^ 0 = 1) |
| 3 | Assume we have the number 2000 - we can calculate the base 36 value as follows: 2000 / 1296 = 1 remainder 704. 704 / 36 = 19 remainder 20. 20 / 1 = 20. The 3 values 1, 19 and 20 hence correspond to the index values 1, J and K - i.e. 2000 in base 10 = 1JK in base 36 |
| 5 | |
| 6 |