Using a 302 response code, each time calling https://tinyurl.com/{shortened_Url} will call the server. It adds to the server load. However, if statistics about the short url is needed, then 301 will be better.
classUrlShortener:def__init__():# Suppose that the hash seed is based on the auto-increment global id self.globalID =0# Suppose that we truncate the hashed url by the mapping between short and long url self.shortToLongMap =dict()# Hardcode constants self.base62Str ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" self.shortLength =6 self.domainName = https://tinyurl.com/" # Suppose that we need to store the url map deflongToShort(longUrl:str) ->str: self.globalID +=1 currId = self.globalID shortened =""while currId >0: shortened ="" index = currId %62 shortened += self.base62Str[index] currId = currId //62iflen(shortened)< self.shortLength: shortened ="0"* (self.shortLength -len(shortened)) + shortened self.shortToLongMap[shortened]= longUrlreturn self.domainName + shorteneddefshortToLong(shortUrl:str) ->str: shortened = shortUrl[len(self.domainName):]return self.shortToLongMap[shortUrl]