This commit is contained in:
Kevin Xu
2019-05-30 13:08:20 +08:00
parent ca677e0701
commit 4a02c5d0f1

View File

@@ -130,7 +130,7 @@ To generate the unique url, we could:
* Base 64 is another popular encoding but provides issues for urls because of the additional `+` and `/` characters * Base 64 is another popular encoding but provides issues for urls because of the additional `+` and `/` characters
* The following [Base 62 pseudocode](http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener) runs in O(k) time where k is the number of digits = 7: * The following [Base 62 pseudocode](http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener) runs in O(k) time where k is the number of digits = 7:
``` ```python
def base_encode(num, base=62): def base_encode(num, base=62):
digits = [] digits = []
while num > 0 while num > 0
@@ -142,13 +142,13 @@ def base_encode(num, base=62):
* Take the first 7 characters of the output, which results in 62^7 possible values and should be sufficient to handle our constraint of 360 million shortlinks in 3 years: * Take the first 7 characters of the output, which results in 62^7 possible values and should be sufficient to handle our constraint of 360 million shortlinks in 3 years:
``` ```python
url = base_encode(md5(ip_address+timestamp))[:URL_LENGTH] url = base_encode(md5(ip_address+timestamp))[:URL_LENGTH]
``` ```
We'll use a public [**REST API**](https://github.com/donnemartin/system-design-primer#representational-state-transfer-rest): We'll use a public [**REST API**](https://github.com/donnemartin/system-design-primer#representational-state-transfer-rest):
```shell ```
$ curl -X POST --data '{ "expiration_length_in_minutes": "60", \ $ curl -X POST --data '{ "expiration_length_in_minutes": "60", \
"paste_contents": "Hello World!" }' https://pastebin.com/api/v1/paste "paste_contents": "Hello World!" }' https://pastebin.com/api/v1/paste
``` ```
@@ -182,8 +182,8 @@ Response:
``` ```
{ {
"paste_contents": "Hello World", "paste_contents": "Hello World"
"created_at": "YYYY-MM-DD HH:MM:SS", "created_at": "YYYY-MM-DD HH:MM:SS"
"expiration_length_in_minutes": "60" "expiration_length_in_minutes": "60"
} }
``` ```
@@ -194,7 +194,7 @@ Since realtime analytics are not a requirement, we could simply **MapReduce** th
**Clarify with your interviewer how much code you are expected to write**. **Clarify with your interviewer how much code you are expected to write**.
``` ```python
class HitCounts(MRJob): class HitCounts(MRJob):
def extract_url(self, line): def extract_url(self, line):