做题2015.3.27

来源:互联网 发布:python import thread 编辑:程序博客网 时间:2024/06/10 16:50

题目描述:

     在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

例如下面的二维数组就是符合这种约束条件的。如果在这个数组中查找数字7,则返回true;如果查找数字5,由于数组不含有该数字,则返回false。

  1
  2    8
  9
  2
  4
  9  12
  4  7
  10    13
  6
  8
  11    15


系统设计题
微博中的url往往很长,发送前要转化为tinyurl
1、url如何转为tinyurl编码


例子:http://dou.bz/1WEhro

该系统使用6个短码字符来表示任何长度的网址。 有效的字符代码是ASCII 'A'到'Z'和'0'的'5',其中每个字符包含2 ^ 5(32)状态。  6短码字符可用于绘制32 ^ 6(1073741824)的网址 


loop1: while true loop1: 
  calculate md5 of the URL 
  loop2: from 1st 4 bytes to 4th 4 bytes of md5 result loop2: 
    cast the 4 bytes to an integer 
    loop3: for shortCodeChar[0] to shortCodeChar[5] 
      use 1st 5 bits of the integer to find the value in codeMap 
      remove 5 bits from the integer 
    end loop3 
    save shortCodeChar as shortCode 
    if shorCode does not exist in database 
      insert the short code and original URL into database 
      break loop1: 
    else 
      retrieve the stored URL from database 
      if original URL equals to URL stored in database 
        break loop1: 
      end if 
    end if 
  end loop2 
  insert '-' 
end loop1 
return shortCode 


Note: codeMap contains value of valid characters from 'a' to 'z' (index 0 to 25) and '0'-'5' (index 26 to 31).


2、如果用户输入一个已经转换过的URL,如何快速定位到已经生成了的tinyurl
3、如果数据为10亿条,需要10个tinyurl服务器,怎么设计?

0 0