几道经典面试题

1、怎么判断所有的服务访问的是否是同一个浏览器

2、怎么通知所有服务,目前的登录状态

3、用户和浏览器是多对多的状态

4、cookie 和 session 基本可以不用考虑

5、请讲一讲HTTP的过程以及应用

6、数据库中有100w条数据,其中有一部分重复数据,不知道重复多少条,怎样通过一个SQL删除所有的重复数据,且留下重复数据中的一条。

delete from order where order_id In (select order_id from order where order_id in (select count(order_id) as num, order_id from order group by order_id having num > 1) as tmp)

7、外观数列:是一个整数序列,从数字1开始,序列中的每一项都是对前一项的描述。前五项如下:

1.  1
2.  11
3.  21
4.  1211
5.  111221

1 被读作 “one 1” (“一个一”),即11。

11 被读作“two 1”("两个一"),即21。

21 被读作“one 2”,“one 1” ("一个二",“一个一”),即1211。

给定一个正整数n(1=<n=<30),输出外观数列的第n项。

注意:整数序列中的每一项将表示为一个字符串。

# 方案一:
from itertools import groupby
def countAndSay(n: int, res='1') -> str:
    s = ''
    if n == 1:
        return res
    else:
        for val, gen in groupby(res):
            s = s.join(str(len(list(gen)))) + val
        return countAndSay(n - 1,s)

print(countAndSay(3))

# 方案二:
def countAndSay( n: int) -> str:
    def trans(string):
        res = ''
        i = 0
        j = i
        while i < len(string):
            while j < len(string):
                if string[i] == string[j]:
                    j += 1
                else:
                    break
            res += str(j - i) + string[i]
            i = j
        return res

    if n == 1:
        return '1'
    return trans(countAndSay(n - 1))
print(countAndSay(5))

8.假设数据库中的某个表有一千万条数据,要求你给这个表添加一个字段,尽可能多的写出你的方案。

# 方案一: 直接添加,表数据量比较小的情况下;线上的一张表如果数据量很大呢,执行加字段操作就会锁表,这个过程可能需要很长时间甚至导致服务崩溃,那么这样操作就很有风险了。
alter table table_name add 字段名 字段类型(宽度)
alter table  user add phone char(11)   # 最好不要加默认值,不然时间会延长。

# 方案二:拷贝原表结构,添加新字段,然后将老表内容复制到新表
① 创建一个临时的新表,首先复制旧表的结构(包含索引)
create table new_table like old_table;
② 给新表加上新增的字段
alter table  new_table add phone char(11) 
③ 把旧表的数据复制过来
insert into new_table(filed1,filed2…) select filed1,filed2,… from old_table
④ 删除旧表,重命名新表的名字为旧表的名字
不过这里需要注意,执行第三步的时候,可能这个过程也需要时间,这个时候有新的数据进来,所以原来的表如果有字段记录了数据的写入时间就最好了,可以找到执行这一步操作之后的数据,并重复导入到新表,直到数据差异很小。不过还是会可能损失极少量的数据。
所以,如果表的数据特别大,同时又要保证数据完整,最好停机操作。

# 方案三:在从库进行加字段操作,然后主从切换

9.1 给列表中的字典排序,假设有如下list对象,alist=[{"name": "a", "age": 20},{'name':"b","age":28},{'name':'c','age':23},{'name':'d','age':30}],将alist中的元素按照age从大到小排序。

sorted_l = sorted(alist,key=lambda  dic:dic['age'],reverse=True)
print(sorted_l)
'''
[{'name': 'd', 'age': 30}, {'name': 'b', 'age': 28}, {'name': 'c', 'age': 23}, {'name': 'a', 'age': 20}]
'''

9.2 给下面的字典套字典按照age进行从小到大排序。

dic1 = {
    'a':{'name':'zhang','age':16},
    'b':{'name':'zhaoliang','age':11},
    'c':{'name':'nange','age':28},
}
print(dict(sorted(dic1.items(),key=lambda dic2:dic2[1]['age'])))
'''
{
'b': {'name': 'zhaoliang', 'age': 11}, 
'a': {'name': 'zhang', 'age': 16},
'c': {'name': 'nange', 'age': 28},
}
'''

10.加入有如下两个list,l1 = ['a','b','c','d'],l2 = [1,2,3,4],将a中的元素作为key,b中的元素作为把value,将a,b合并为字典。

# 方式一:
d = {}
l1 = ['a','b','c','d']
l2 = [1,2,3,4]
for i in range(len(l1)):
    d[l1[i]] = l2[i]
print(d)

# 方式二:
print(dict(list(zip(l1,l2))))

'''
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
'''
  1. list1 = ['a','b','c'],list2 = [1,2,3],以list1的元素为键,以list2的元素为值,生成一个字典,并按照从键小到大的顺序排序
list_key = ['a','b','c']
list_value = [1,2,3]
dict1 = {k:v for k,v in sorted(zip(list_key,list_value),reverse=True)}
print(dict1)

12.使用python已有的数据结构,简单实现一个栈结构

# 栈: 先进后出  栈就是堆栈
l =[1,2,3,4,5]
l.append(6)
l.append(7)
while True:
    try:
        res = l.pop()
        print(res)
    except:
        print('列表已经空了!')
        break

        
# 队列:先进先出
from collections import deque
queue = deque(l)
queue.append(4)
queue.append(5)
print(queue)  # deque([1, 2, 3, 4, 5])
print(queue.popleft())  # 1
print(queue.popleft())  # 2
print(queue)  # deque([3, 4, 5])

13.统计list中单词及其出现次数

a=['apple','banana','orange','apple','tomato','orange','apple','banana','watermelon']
d={}

for word in a:
    if word not in d:
        d[word] = 1
    else:
        d[word] += 1
print(d)
'''
{'apple': 3, 'banana': 2, 'orange': 2, 'tomato': 1, 'watermelon': 1}
'''

14.从一个字符串中提取出链接地址

s = 'Runoob 的网页地址为:https://www.runoob.com,Google 的网页地址为:https://www.google.com'
import re
url = re.findall('https?://(?:[-\w.]|(?:%[\da-zA-Z]{2}))+',s)
print(url)  # ['https://www.runoob.comGoogle', 'https://www.google.com']
  1. 使用代码求出用户的共同好友个数?(可以通过sql或者django查询处理)
# 建表并插入数据  在数据库db3中
create table Follow(
    follow_id int primary key auto_increment,
    user_id varchar(64),
    usee_id varchar(64),
    type int,
    status tinyint(1)
);

insert into Follow values(1,'abc','def',1,1),(2,'abc','dcf',1,1),(3,'abc','dbf',1,1),(4,'def','dcf',1,1),(5,'def','abc',1,1),(6,'def','ccd',1,1),(7,'zxa','abc',1,1);
posted on 2020-08-02 23:08  jueyuanfengsheng  阅读(106)  评论(0编辑  收藏  举报