Final challenge: a smaller shortener
After this: Estimate, encode, complete and size a short-link service of your own.
This challenge has five steps, and each step is scored on its own. You will estimate the redirects of a smaller shortener, predict a base62 code, complete the line that builds a code, explain a wrong number from Java, and work out the database reads a cache leaves. The steps with code show Python and Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Estimate the reads
Estimate the reads
A smaller shortener makes 10 million new links a month, and each link is opened 50 times. About how many redirects a second is that? A 30-day month has 2,592,000 seconds.
Predict the code
The base62 function from lesson 3, called with 1000. What does this print?
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62(n): if n == 0: return "0" code = "" while n > 0: code = ALPHABET[n % 62] + code n //= 62 return code
print(base62(1000))public class Main { static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String base62(long n) { if (n == 0) { return "0"; } String code = ""; while (n > 0) { code = ALPHABET.charAt((int) (n % 62)) + code; n /= 62; } return code; }
public static void main(String[] args) { System.out.println(base62(1000)); }}Complete the loop
Complete the line that puts the next character in front of the code, so that the program prints g8.
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def base62(n): if n == 0: return "0" code = "" while n > 0: ________ # ← put the next character in front n //= 62 return code
print(base62(1000))public class Main { static final String ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static String base62(long n) { if (n == 0) { return "0"; } String code = ""; while (n > 0) { ________ // ← put the next character in front n /= 62; } return code; }
public static void main(String[] args) { System.out.println(base62(1000)); }}A wrong number
In Java, int codes = 62 * 62 * 62 * 62 * 62 * 62; followed by a print shows 965660736. Python prints 56800235584 for the same product. What happened in Java?
Size the database reads
The smaller shortener from step 1 has about 193 redirects a second. With a cache at a hit ratio of 0.9, about how many redirects a second reach the database?
- Estimate the redirects a second of a smaller shortener from its links a month and visits per link.
- Predict a base62 code by hand, and complete the line of the loop that puts each character in front.
- Explain why Java prints 965660736 for 62 to the power 6 as an int, and work out the database reads a cache leaves.