Quick row etc.

Want to see the opportunity? Are the following 10 most frequent hand-tear code interview questions all available?

 

Believe me, if you thoroughly master the solutions to the following 10 questions, the probability of you successfully completing the hand-tear code interview questions is at least 50%.

 

1. Quick sort

Question format: Write a quick sort algorithm by hand.

 

Problem Difficulty: Moderate.

 

Occurrence probability: about 50%. Handwritten quick platoon is definitely the king of beasts in the hand-tear code interview questions. If you master it, you will send points. If you don't master it, you will send propositions.

 

Reference Code:

 

def quick_sort(arr,start=0,end=None):
    if end is None:
        end = len(arr)-1
    if end<=start:
        return(arr)
    i,j = start,end
    ref = arr[start]
    while j>i:
        if arr[j]>= ref:
            j = j - 1
        else:
            # 此处使用一行语句交换3个元素的值
            arr[i],arr[j],arr[i+1] = arr[j],arr[i+1],arr[i]
            i = i + 1
    quick_sort(arr,start=start,end = i-1)
    quick_sort(arr,start=i+1,end = end)
    return(arr)

print(quick_sort([1,1,3,3,2,2,6,6,6,5,5,7]))

 

Output result:

[1, 1, 2, 2, 2, 3, 5, 5, 6, 6, 6, 7]

 

2, binary search

Question format: Write a binary search algorithm by hand. Given an ordered array arr and a target element target, return the index of target in arr, or -1 if it does not exist.

 

Problem Difficulty: Easy.

 

Occurrence probability: about 30%. After the binary search is definitely the beast in the handwritten code question, no concubine can compete with her for favor. Even if you can't write a binary search, you are still applying for programmers. Do you have any misunderstandings about the profession of programmers?

 

Reference Code:

 

def binary_search(arr,target):
    start,end = 0,len(arr)-1
    while True:
        if end - start <=1:
            if target == arr[start]:
                return(start)
            elif target == arr[end]:
                return(end)
            else:
                return(-1)

        mid = (start + end)//2
        if arr[mid]>=target:
            end = mid
        else:
            start = mid

print(binary_search([1,4,7,8,9,12],9))
print(binary_search([1,4,7,8,9,12],3))

 

Output result:

4-1

 

3. Climbing the stairs

Question format: There is a staircase with a total of 10 steps. You can only walk one or two steps at a time. After all the steps are completed, how many ways are there?

 

Problem Difficulty: Easy.

 

Occurrence probability: about 20%. The problem of climbing stairs is the leopard of beasts in the handwritten code problem. The stair-climbing problem can be solved with recursion, but if the time complexity is considered, it is best to use the idea of ​​dynamic programming, which is a textbook-level case of dynamic programming algorithms. I don't even understand how to climb a staircase. The basis of this algorithm is worrying!

 

Reference Code:

 

def climb_stairs(n):
    if n==1:
        return 1
    if n==2:
        return 2
    a,b = 1,2
    i = 3
    while i<=n:
        a,b = b,a+b
        i +=1
    return b

print(climb_stairs(10))

 

Output result:

89

 

4, the sum of two numbers

Question form: Find the subscript of the element in the list that satisfies the sum of two numbers equal to the target value. For example: arr = [2,7,4,9], target = 6 returns [0,2], if it does not exist, returns an empty list [].

 

Problem Difficulty: Easy.

 

Occurrence probability: about 20%. The sum of the two numbers is the wolf of the beasts in the handwritten code question. The sum of two numbers problem examines the candidate's understanding of the property that hash tables can trade space for time. Hey, writing the sum of two numbers still involves two loops. What is the time complexity for you?

 

Reference Code:

 

def sum_of_two(arr,target):
    dic = {}
    for i,x in enumerate(arr):
        j = dic.get(target-x,-1)
        if j != -1:
            return((j,i))
        else:
            dic[x] = i
    return([])

arr = [2,7,4,9]
target = 6
print(sum_of_two(arr,target))

 

Output result:

(0, 2)

 

5. Maximum drawdown

Question form: There is an array, find two numbers x, y, satisfy the index of x is less than the index of y, so that xy is the largest. For example arr = [3,7,2,6,4,1,9,8,5], the maximum drawdown is 6, corresponding to x=7,y=1.

 

Problem Difficulty: Moderate.

 

Occurrence probability: about 20%. This question can come in various forms such as the best time to buy or sell a stock, or the maximum lift, which is also an epic example of dynamic programming. Yo, there are two more loops, and this loop is very well written.

 

Reference Code:

 

def max_drawdown(arr):
    assert len(arr)>2, "len(arr) should > 2!"
    x,y = arr[0:2]
    xmax = x
    maxdiff = x-y

    for i in range(2,len(arr)):
        if arr[i-1] > xmax:
            xmax = arr[i-1]
        if xmax - arr[i] > maxdiff:
            maxdiff = xmax - arr[i]
            x,y = xmax,arr[i]

    print("x=",x,",y=",y)
    return(maxdiff)

print(max_drawdown([3,7,2,6,4,1,9,8,5]))

 

Output result:

x= 7 ,y= 16

 

6. Merge two sorted arrays

Question Form: Given two sorted arrays in ascending order, merge them into a new sorted array. For example: a = [1,2,6,8], b = [2,4,7,10], the output is arr = [1,2,2,4,6,7,8,10]

 

Problem Difficulty: Easy.

 

Occurrence probability: about 15%. This topic examines the basic idea of ​​merge sort. Note that these two arrays are ordered, how can you ignore such favorable conditions and directly splice the two arrays and start bubbling? ? ?

 

Reference Code:

 

def merge_sorted_array(a,b):
    c = []
    i,j = 0,0
    while True:
        if i==len(a):
            c.extend(b[j:])
            return(c)
        elif j==len(b):
            c.extend(a[i:])
            return(c)
        else:
            if a[i]<b[j]:
                c.append(a[i])
                i=i+1
            else:
                c.append(b[j])
                j=j+1

print(merge_sorted_array([1,2,6,8],[2,4,7,10]))

 

Output result:

[1, 2, 2, 4, 6, 7, 8, 10]

 

7, the largest contiguous subarray and

Question Form: Given an array, find the sum of its largest contiguous subarrays. For example: arr = [1,5,-10,2,5,-3,2,6,-3,1]. The output is: 12. The corresponding contiguous subarray is [2,5,-3,2,6].

 

Problem Difficulty: Moderate.

 

Occurrence probability: about 15%. This problem is also an ancestral example of dynamic programming. Classmates, your double-loop writing is really 6, but we can't think that your question is right!

 

Reference Code:

 

def max_sub_array(arr):
    n = len(arr)
    maxi,maxall = arr[0],arr[0]
    for i in range(1,n):
        maxi = max(arr[i],maxi + arr[i])
        maxall = max(maxall,maxi)
    return(maxall)

print(max_sub_array([1,5,-10,2,5,-3,2,6,-3,1]))

 

Output result:

12

 

8, the longest non-repeating substring

Question Form: Given a string, find the longest substring without repeating characters. For example, enter "abcbefgf" and the answer is "cbefg".

 

Problem Difficulty: Difficult.

 

Occurrence probability: about 10%. This is a comprehensive application problem of dynamic programming + hash lookup. This question can be done, your code skills are very good. By the way, what is your expected salary?

 

Reference Code:

 

def longest_substr(s):    
    dic = {}
    start,maxlen,substr = 0,0,""

    for i,x in enumerate(s):
        if x in dic:
            start = max(dic[x]+1,start)
            dic[x] = i   
        else:
            dic[x] = i

        if i-start+1>maxlen:
            maxlen = i-start+1
            substr = s[start:i+1]
    return(substr)

print(longest_substr("abcbefgf"))
print(longest_substr("abcdef"))
print(longest_substr("abbcddefh"))

 

Output result:

cbefgabcdefdefh

 

9, full arrangement

Question Form: Given an array, find all possible permutations of it. For example: arr = [1,1,3], the output is [[1,1,3],[1,3,1],[3,1,1]].

 

Question Difficulty: Moderate

 

Occurrence probability: about 10%. This is a comprehensive application problem of dynamic programming + permutation combination. Classmates, what is your time complexity if you use recursion here? Once our array has dozens of elements, can you still run?

 

Reference Code:

 

import numpy as np 
def permutations(arr):
    if len(arr)<=1:
        return([arr])
    t = [arr[0:1]]
    i = 1
    while i<=len(arr)-1:
        a = arr[i]
        t = [xs[0:j]+[a]+xs[j:] for xs in t for j in range(i+1)]
        t = np.unique(t,axis=0).tolist()
        i = i+1
    return(t)
print(permutations([1,1,3]))

 

Output result:

[[1, 1, 3], [1, 3, 1], [3, 1, 1]]

 

10, Sansanowa

Question form: Given an array and target number target, find all combinations of a, b, and c in the array that satisfy a+b+c = target. For example: arr = [-3,-1,-2,1,2,3], target = 0. The output is [(-3,1,2),(-2,-1,3)]

 

Question Difficulty: Difficult

 

Occurrence probability: about 5%. This is a very tricky subject. You can try sorting arr first. Note that our time complexity requirement is O(n**2), and the space complexity requirement is O(1), yes, it is so strict, you have to think about it... yo, have an idea...emmm...generally Meet the requirements... Classmates, do you have any other offers in hand?

 

Reference Code:

 

def sum_of_three(arr,target):
    assert len(arr)>=3,"len(arr) should >=3!"
    arr.sort()
    ans = set()
    for k,c in enumerate(arr):
        i,j = k+1,len(arr)-1
        while i<j:
            if arr[i]+arr[j]+c <target:
                i = i+1
            elif arr[i]+arr[j]+c > target:
                j = j-1
            else:
                ans.update({(arr[k],arr[i],arr[j])})
                i = i+1
                j = j-1
    return(list(ans))

print(sum_of_three([-3,-1,-2,1,2,3],0))

 

Output result:

[(-2, -1, 3), (-3, 1, 2)]

●Number 757, enter the number directly to this article

●Enter m to get the article directory

Reprinted in: https://www.cnblogs.com/whnbky/p/11549710.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326559978&siteId=291194637
Row
Recommended