[LeetCode] 534. Design TinyURL 设计短网址

Note: For the coding companion problem, please see: Encode and Decode TinyURL.

How would you design a URL shortening service that is similar to TinyURL?

Background:
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.

Requirements:

  1. For instance, "http://tinyurl.com/4e9iAk" is the tiny url for the page "https://leetcode.com/problems/design-tinyurl". The identifier (the highlighted part) can be any string with 6 alphanumeric characters containing 0-9a-zA-Z.
  2. Each shortened URL must be unique; that is, no two different URLs can be shortened to the same URL.

Note about Questions:
Below are just a small subset of questions to get you started. In real world, there could be many follow ups and questions possible and the discussion is open-ended (No one true or correct way to solve a problem). If you have more ideas or questions, please ask in Discuss and we may compile it here!

Questions:

      1. How many unique identifiers possible? Will you run out of unique URLs?
      2. Should the identifier be increment or not? Which is easier to design? Pros and cons?
      3. Mapping an identifier to an URL and its reversal - Does this problem ring a bell to you?
      4. How do you store the URLs? Does a simple flat file database work?
      5. What is the bottleneck of the system? Is it read-heavy or write-heavy?
      6. Estimate the maximum number of URLs a single machine can store.
      7. Estimate the maximum number of queries per second (QPS) for decoding a shortened URL in a single machine.
      8. How would you scale the service? For example, a viral link which is shared in social media could result in a peak QPS at a moment‘s notice.
      9. How could you handle redundancy? i,e, if a server is down, how could you ensure the service is still operational?
      10. Keep URLs forever or prune, pros/cons? How we do pruning? (Contributed by @alex_svetkin)
      11. What API would you provide to a third-party developer? (Contributed by @alex_svetkin)
      12. If you can enable caching, what would you cache and what‘s the expiry time? (Contributed by @Humandroid)

系统设计题,代码只是其中的一部分,估计大部分还是要跟面试官侃大山,参考大牛的文章吧:

https://segmentfault.com/a/1190000006140476

原文地址:https://www.cnblogs.com/lightwindy/p/8481764.html

时间: 2024-11-06 21:08:59

[LeetCode] 534. Design TinyURL 设计短网址的相关文章

[LeetCode] 355. Design Twitter 设计推特

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods: postTweet(userId, tweetId): Compo

LeetCode 705. Design HashSet (设计哈希集合)

题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java Solution: Runtime: 58 ms, faster than 90.21% Memory Usage: 56.3 MB, less than 68.53% 完成日期:03/18/2019 关键点:boolean array class MyHashSet { boolean [] se

LeetCode 706. Design HashMap (设计哈希映射)

题目标签:HashMap 题目让我们设计一个 hashmap, 有put, get, remove 功能. 建立一个 int array, index 是key, 值是 value,具体看code. Java Solution: Runtime: 76 ms, faster than 27.53% Memory Usage: 58.2 MB, less than 31.57% 完成日期:03/18/2019 关键点:int array class MyHashMap { int [] map;

短网址服务设计

短网址服务设计 背景 短网址服务,用来将输入的一个长网址转换为一个短网址(比如附录中的案例),当用户请求这个短网址时,服务查询出真实的url; 设计这么一个短网址服务,需要考虑哪些点? 数据结构 首先,需要考虑短网址应该如何存储,使用一个key-value结构就可以: key是生成的短网址,具有唯一性: value为原始真实网址: 算法 计算短网址的算法可以很简单,短网址与原始网址就只存在一个映射关系. 从1开始递增来映射每一个网址: 1个位上可以使用26位字母+10个数字,即36进制: 当然,

URL短网址系统的算法设计及实践

在通常情况下,URL是由系统生成的,通常包括URI路径,多个查询参数,可以对参数进行加密和解密.当人们要分享某个URL,比如短信,邮件,社交媒体,这就需要短URL. 而短网址,顾名思义就是在长度上比较短的网址.简单来说就是帮您把冗长的URL地址缩短成8个字符以内的短网址. 你有没有遇到过短信字符过长本来一条信息搞定的事情需要发两条?你有没有遇到过填报系统里填写网址却因为字符限制无法完整提交?你有没有遇到过排版好的内容由于链接过长而完全打乱了美感? 这时候,你就需要用到短网址生成工具了. 如开发者

最近做了一个短网址服务 di81.com

最近做了一个短网址服务:   di81.com 项目中有一处需求,需要把长网址缩为短网址,把结果通过短信.微信等渠道推送给客户.刚开始直接使用网上现成的开放服务,然后在某个周末突然手痒想自己动手实现一个别具特色的长网址(文本)缩短服务. 由于以前做过socket服务,对数据包的封装排列还有些印象,因此,短网址服务我第一反应是先设计数据的存储格式,我这里没有采用数据库,而是使用2个文件来实现: Url.db存储用户提交的长网址文本,Url.idx  存储数据索引,记录每次提交数据的位置(Begin

百度短网址服务

百度短网址服务api文档 String shareUrl = "http://www.baidu.com/"; RestTemplate template = new RestTemplate(); //百度短地址服务 String url = "http://dwz.cn/create.php"; LinkedMultiValueMap<String, Object> parts = new LinkedMultiValueMap<String,

长链接转短链接(短网址)

场景 ? 短链接服务就是将一段长的URL转换为短的URL,比如利用新浪微博的短链接生成器,可将一段长的URL(http://blog.csdn.net/poem_qianmo/article/details/52344732)转换为一段短的URL(http://t.cn/RtFFvic),用户通过访问短链接即可重定向到原始的URL. 整个交互流程如下: 用户访问短链接:http://t.cn/RtFFvic 短链接服务器t.cn收到请求,根据URL路径RtFFvic获取到原始的长链接:http:

六度短网址对比百度统计、友盟有什么不同?

你还在用百度统计吗?有这个统计工具,不懂代码能快速上手使用 知道六度短网址的朋友一般都是因为它的缩短网址功能,其实它还有另外一个非常非常非常宝藏的用途——做网站访问分析.   一.什么是网站访问分析?   是指在获得网站流量统计基本数据的前提下,对有关数据进行统计.分析,从中发现用户访问网站的规律,并将这些规律与网络营销策略等相结合,从而发现目前网络营销活动中可能存在的问题,并为进一步修正或重新制定网络营销策略提供依据.   二.网站访问分析的意义   (1) 了解网站的目标用户画像,为产品设计