educative.io

How does KGS works?

@Design_Gurus There is no algo defined to generate 6 digit random key in the book. In the algo section it just tells various problems and then it says kgs can solve.How exactly KGS solves, is not answerd?

  1. Why cant we use DB auto increment as a random key?

  2. How exactly is KGS gonna work? If it generates random number and then check if it is unique or not, then it is basically doing same thing, but just doing offline.How does it solve problem?

I have the same doubt…
So generating short url journey starts below —

  • using md5 hash function for hashing the original URL
  • since above md5 will create 128 bit and we need short 6 character url so used base 64 to encode the 128 bit.
  • Still it gives 21 character so went to selecting last 6 characters.
  • Still this will create duplicate issue so went randomizing characters .
  • Still there will good chance there might be duplicate key
  • So started adding the user_id to the original url and then do above (base64(md5(original-rl)) % 6)
  • Still this will add problem of only logged in user can generate the short url.
  • Then jumped to KGS…
    … DIrectly this statement : that generates random six-letter strings beforehand and stores them in a database (let’s call it key-DB).

I mean but how ?..