寒假实习面试算法岗总结(持续更新.....)

寒假以来一直在投一些算法实习岗位,既然是算法,面大厂的话肯定少不了“手撕代码”的问题,怕自己忘掉,因此做已记录。目标是面试8个公司,现在已经面试了有三星、华为、快手、深信服、字节跳动,其中三星没有考代码题目。

三星

比较水,没有考代码问题。

华为

比较简单,就考察了一个斐波那契数列的问题。面试也是比较简单只考了一个算法题。


class Solution:
    def fib(self, n: int) -> int:
        a, b = 0, 1
        for _ in range(n):
            a, b = b, a + b
        return a

快手

目前已经面了两面了,结果还不清楚,两面总共考了4到代码题,只记得三道了

1、读取一个文件的图像,然后将结果作为数组输出。

import cv2
img_BGR = cv2.imread(path)
img = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
#返回的结果是numpy类型,转为list就可以了
print(img.tolist())

2、如何判断两个二叉树完全一致,节点的value也是一样的。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not  p and not q:
            return True
        if not p or not q:
            return False
        #递归
        return p.val==q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
        
        #迭代
        res_p,res_q = [],[]
        def helper(root,res):
            queue = collections.deque()
            queue.append(root)
            while queue:
                item = queue.popleft()
                if item:
                    res.append(item.val)
                    queue.append(item.left)
                    queue.append(item.right)
                else:
                    res.append('null')
        helper(p,res_p)
        helper(q,res_q)
 
        return res_p==res_q

3、查找一个字符串中无重复字符的最长子串长度。

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if not s:
            return 0
        res = 1
        for i in range(len(s)):
            tmp = set()
            tmp.add(s[i])
            for j in range(i+1,len(s)):
                if s[j] not in tmp:
                    tmp.add(s[j])
                else:
                    break
            res =  max(res,len(tmp))
        
        return res


        k = -1
        res = 0
        c_dict = {}
        for i, c in enumerate(s):
            #如果出现重复的元素且下标是大于k
            #更新k,更新c的下标
            if c in c_dict and c_dict[c] > k: 
                k = c_dict[c]
                c_dict[c] = i
            #如果没有出现重复的元素,直接给字典添加并且计算更新res的长度
            else:
                c_dict[c] = i
                res = max(res, i-k)
        return res
        

深信服

  • 讲讲SVM和逻辑回归的区别以及底层的原理

  • 讲讲RNN和LSTM的原理和优缺点

  • 讲讲你用过的损失函数,优化函数的作用

  • 讲讲你用过的优化器,SGD和Adam。两个的底层原理是什么?

    扫描二维码关注公众号,回复: 13012646 查看本文章
  • CNN为啥比RNN快

  • CNN的细节实现什么

  • 讲讲你的项目,从背景、应用来说,具体将的话从输入、输出和目标函数来讲。不应该一开始就讲细节

  • 聊聊Python:一行代码交换两个变量,生成器和yield的原理

  • L1正则化和L2正则化的区别是啥?为啥L2能好一些,对于L1有哪些提升?

  • 二面到此结束、觉得自己菜的扣脚、流下了基础薄弱的眼泪;总结下来就是底层、底层的问题、基础太差了!!!好多问题回答的含糊不清;       

字节跳动

一面:讲项目或者论文,我讲了一个我正在做的论文,问的比较详细,大概聊了30多分钟吧,然后写代码。比较简单:无重复字符的最长子串。

我面试的时候写的方法3,面试官让我下去在想想如何优化,优化版就是前两个。第二个利用双指针+hash表比较有意思,能加快效率。

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        
        #滑动窗口
        #维护一个窗口,必须保证每种元素只出现了一次
        res = 0
        win = []
        for char in s:
            #如果字符不在窗口,那就他添加字符
            if char not in win:
                win.append(char)
            #如果字符在窗口里,那就截取第一次出现字符之后作为新的窗口
            #并将当前的元素添加进去
            else:
                win = win[win.index(char)+1:]
                win.append(char)
            
            #更新窗口的大小
            res = max(res,len(win))
        
        return res





        #hash+双指针
        dic = defaultdict(int)
        res = 0
        index = -1
        #维护一个字典,key是字符,value是下标
        for j in range(len(s)):
            #如果当前字符在字典中的话,那就更新下标,将左指针向右移动
            if s[j] in dic:
                index =max(dic[s[j]],index)
            #更新字典
            dic[s[j]] = j
            #更新子字符串的长度
            res = max(res,j-index)
        return res




        #滑动窗口的模板
        res = 0
        left,right = 0,0
        size = len(s)
        counter = collections.Counter()

        while right<size:
            counter[s[right]]+=1
            #当出现频次最高的字符频次大于1的时候,那就更新字典、移动左指针
            while counter.most_common(1)[0][1]>1:
                counter[s[left]]-=1
                left+=1
            
            res = max(res,right-left+1)
            right+=1
        
        return res

未完...... 持续更新

猜你喜欢

转载自blog.csdn.net/weixin_37724529/article/details/113351653
今日推荐