Summary of winter vacation internship interview algorithm post (continuous update...)

Since the winter vacation, I have been investing in some algorithm internship positions. Since it is an algorithm, the problem of "hand tearing code" must be indispensable for large companies. I am afraid I will forget it, so I have recorded it. The goal is to interview 8 companies. Now we have interviewed Samsung, Huawei, Kuaishou, Shenxin, and ByteDance. Samsung has no code questions.

Samsung

Comparing water, there is no code problem.

Huawei

It is relatively simple, and it examines a Fibonacci sequence problem. The interview is relatively simple and only one algorithm question is tested.


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

quick worker

I have already faced both sides, and the result is not clear. A total of 4 code questions have been tested on both sides, and I only remember three.

1. Read the image of a file, and then output the result as an array.

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

2. How to judge that two binary trees are exactly the same, and the value of the node is also the same.

# 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. Find the longest substring length without repeated characters in a string.

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
        

Convinced

  • Talk about the difference between SVM and logistic regression and the underlying principles

  • Talk about the principles, advantages and disadvantages of RNN and LSTM

  • Talk about the loss function you have used, the role of the optimization function

  • Tell me about the optimizers you have used, SGD and Adam. What is the underlying principle of the two?

  • Why is CNN faster than RNN

  • What does the details of CNN achieve

  • Speaking of your project, in terms of background and application, and specifically in terms of input, output and objective function. Shouldn't talk about the details at the beginning

  • Talk about Python: the principle of swapping two variables in one line of code, generator and yield

  • What is the difference between L1 regularization and L2 regularization? Why is L2 better, and what are the improvements to L1?

  • This is the end of the two sides, and I feel that my cooking has been smashed, and I shed tears of weak foundation; in summary, it is the bottom layer, the bottom layer problem, the foundation is too bad! ! ! Many questions were answered vaguely;       

 

Byte beating

One side: Talking about the project or thesis, I talked about a thesis I am working on, and I asked in more detail. I talked for more than 30 minutes, and then wrote the code. Relatively simple: the longest substring without repeated characters.

I wrote Method 3 during the interview. The interviewer asked me to think about how to optimize. The optimized version is the first two. The second use of double pointer + hash table is more interesting and can speed up efficiency.

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

Unfinished...Continuously updated

Guess you like

Origin blog.csdn.net/weixin_37724529/article/details/113351653
Recommended