记MS面试

我是三月份就做好笔试,由于个人时间问题顺延了,但好像给我标漏了
不管怎样,终于赶上了最后的补录面试。本人作为一只菜鸡做好了两战的准备吧。(实习和秋招
----------------------------分割线------------------------------------------
2021/6/7 11:00-11:45
first round
全程英文面,面试官英语超流利
个人介绍
介绍一个自己的项目以及项目的难点(业务的东西没有深问
编程:给定一个整型数组,找出和为正数的子数组个数。
就首先想到的就是暴力了呗,时间复杂度为O( n 2 n^2 n2),空间复杂度为O(1)

# input: 1 -2 3 -2 
def PSI(nums):
    if not nums: return 0
    window = 0
    res = 0
    for i in range(len(nums)):
        window = nums[i]
        if(window>0): res += 1
        for j in range(i+1,len(nums)):
            window += nums[j]
            if(window>0): res += 1
    return res

print(PSI([1,-2,3,-2]))
print(PSI([-2]))
print(PSI([1,3,4]))
print(PSI([-4763,-43,2448,7609,-5697,721,-1524,582,-1236,4797]))
print(PSI([-1644,-5339,8838,1295,-6961,6367,-6996,7299,8166,-6596,5704,-3379,-1017, -8521, 6888 ,-3618, -7607, 2520, 516, 6240]))

然后面试官提示还有nlogn的解法,我说是排序的做法吗,面试官又提示分治的关键字,merge还是quick sort,难道是quick sort,因为这个题是求子数组所以是连续的,不知为啥我就联系到partition了,面试官说merge,好吧,反正最后我还是想不到怎么用merge。面试后google了一下。
Counting number of contiguous subarrays with positive sum in O(nlogn) using O(n) transformation
Counting inversions in an array
上述两个链接思路就完全出来了:在merge sort过程中保存翻转的个数。我觉得挺巧妙的。

2021/6/7 14:00-14:45
second round
leetcode原题:设计贪吃蛇
这种模拟过程的题目,首先要选好数据结构,其次一定要逻辑清晰
这里self.snake可以用list,也可以用dequeue优化,因为尾部需要进出,头部需要进。

class SnakeGame:

    def __init__(self, width: int, height: int, food: List[List[int]]):
        """
        Initialize your data structure here.
        @param width - screen width
        @param height - screen height 
        @param food - A list of food positions
        E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0].
        """
        self.height = height    #行数
        self.width = width      #列数
        self.food = food
        self.snake = [ [0,0] ]    #起点,初始化
        self.score = 0
        

    def move(self, direction: str) -> int:
        """
        Moves the snake.
        @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 
        @return The game's score after the move. Return -1 if game over. 
        Game over when snake crosses the screen boundary or bites its body.
        """
        nxt_head = self.snake[0][:]   
        # tail = self.snake.pop(-1)   #此时先按照蛇往前走了一步,还没吃东西
        tail = self.snake.pop(-1)
        if direction == "U":
            nxt_head[0] -= 1
        elif direction == "D":
            nxt_head[0] += 1
        elif direction == "L":
            nxt_head[1] -= 1
        else:
            nxt_head[1] += 1

        if nxt_head in self.snake:  #头的下一步咬到自己身子了,咬死了
            return -1
        if not (0 <= nxt_head[0] < self.height and 0 <= nxt_head[1] < self.width):
            return -1       #超界了
        
        self.snake = [nxt_head] + self.snake    #新蛇头,头插
        
        if self.food and nxt_head == self.food[0]:
            self.score += 1
            self.food.pop(0)
            self.snake.append(tail) #蛇头吃到东西了,蛇尾不应该删除,再加回来

        return self.score
        


# Your SnakeGame object will be instantiated and called as such:
# obj = SnakeGame(width, height, food)
# param_1 = obj.move(direction)

这一面表现的不是很好,一个月没刷题了,这个题没做过一上来就有点慌乱,后面感觉自己越来越不自信。但是这轮面试给了我激励,刷题不能松懈,细水长流保持手感。我觉得平时的稳扎稳打很重要,临危不乱即使遇到没见过的也能冷静分析,其实面试官是一直在引导你的。还有就是保持谨慎和解题热情。

----------------------------分割线------------------------------------------

June 10, 2021, 11:00 - 11:45
leader round

上一篇:同调代数笔记6


下一篇:多方安全计算:隐私保护集合求交技术