Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the algorithms to be solved before Python programming interview

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what algorithms to solve before Python programming interview". In daily operation, I believe many people have doubts about what algorithms to solve before Python programming interview. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "what algorithms to solve before Python programming interview"! Next, please follow the small series to learn together!

Why is practicing algorithms key?

don't be naive like I was when I first started solving problems. Although I thought it was fun to crack a few algorithms from time to time, I never spent much time practicing, just solving problems, everything else, maybe sometimes sloppily solving problems, but I didn't understand why. For myself, I keep thinking that at the end of the day, the algorithm is a little too rigid, that it has no practical use in an actual day-to-day work environment, and that it won't do me much good in the long run.

"Knowing how to solve algorithms gives you a competitive advantage in the job search process"

But the reality is that as programmers, complex problems arise on a daily basis, and large companies must find a standardized process to gather insight and attention to detail skills from job seekers. This means that knowing how to solve the algorithm will give you a competitive advantage in the job search process, because even lesser-known companies tend to use similar evaluation methods.

Soon after I started solving algorithms more consistently, I found that there were plenty of resources available to practice, learn the most effective strategies for solving these problems, and mentally prepare for interviews. (e.g. Niuke net, power buckle, collar buckle, etc.)

In addition to practice interview questions, these sites often group algorithms by company, embed active blogs, let people share detailed summaries of their interview experiences, and sometimes even offer mock interview questions as part of advanced programs.

Don't be disappointed if you have a really hard time solving the problem at first, it's perfectly normal. Even very experienced Python programmers will find many algorithms difficult to solve in a short time without adequate training.

Also don't be disappointed if your interview doesn't work out as you expected and you're just starting to work out the algorithm. Some people prepare for months each day to solve a few problems and rehearse regularly before finalizing the interview.

To help you along in your training, below I have selected 10 algorithms (mostly around string operations and arrays) that come up repeatedly in phone coding interviews. The magnitude of these problems is mostly relatively simple, but easy to encounter, so use them as a good starting point.

string operations

reversed digits

#Given an integer, return the number reversed #The number may be negative or integer def solution(x): string = str(x) if string[0] == '-': return int('-'+string[:0:-1]) else: return int(string[::-1]) print(solution(-289)) print(solution(123)) Output: -132 543

Average word length

#Returns the average word length for a given sentence. Remember to delete punctuation first. sentence1 = "Hi all, my name is Tom... I am originally from Australia. " sentence2 = "I need to work very hard to learn more about algorithms in Python! " def solution(sentence): for p in "!? ',;. ": sentence = sentence.replace(p, '') words = sentence.split() return round(sum(len(word) for word in words)/len(words),2) print(solution(sentence1)) print(solution(sentence2)) Output: 4.2 4.08

Algorithms that require you to apply some simple calculations using strings are quite common, so it's important to be familiar with methods such as.replace() and.split(), which in this case helped me remove unwanted characters and create lists of words whose lengths are easily measured and summed.

add string

#Given two non-negative integers num1 and num2 expressed as strings, returns the sum of num1 and num2. #You must not use any of the built-in BigInteger libraries or convert input directly to integers. num1 = '364' num2 = '1836' # Approach 1: def solution(num1,num2): eval(num1) + eval(num2) return str(eval(num1) + eval(num2)) print(solution(num1, num2)) #Approach3 #gives a string of length 1, and ord () returns an integer representing the Unicode code point of the character #when the argument is a unicode object, or a byte value when the argument is an 8-bit string. def solution(num1, num2): n1, n2 = 0, 0 m1, m2 = 10**(len(num1)-1), 10**(len(num2)-1) for i in num1: n1 += (ord(i) - ord("0")) * m1 m1 = m1//10 for i in num2: n2 += (ord(i) - ord("0")) * m2 m2 = m2//10 return str(n1 + n2) print(solution(num1, num2)) Output: 2200 2200

I found both methods equally excellent: the first method was concise and straightforward, using the intuitive eval() method to dynamically evaluate string-based input, and the second method cleverly used the ord() function to reconstruct both methods of string as Unicode code points through which actual numbers pass their characters. If I really had to choose between the two, I might choose the second approach because it seems complicated at first, but is usually handy when solving problems that require more advanced string manipulation algorithms.

Find the first unique character #Given a string, find the first unique character and return its index. #Returns-1 if not present.# Note: All input strings are lowercase. #Approach 1 def solution(s): frequency = {} for i in s: if i not in frequency: frequency[i] = 1 else: frequency[i] +=1 for i in range(len(s)): if frequency[s[i]] == 1: return i return -1 print(solution('alphabet')) print(solution('barbados')) print(solution('crunchy')) print('---') #Approach 2 import collections def solution(s): # build hash map : character and how often it appears count = collections.Counter(s) # 1: # all prime numbers are greater than 1 for i in range(2, num): if (num % i) == 0: # if the modulus == 0 is means that the number can be divided by a number preceding it break else: prime_nums.append(num) return prime_nums solution(n) Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

I want to close with another classic question. If you are familiar with both prime number definitions and modulo operations, you can easily find a solution through the valley range (n)(modulus operation).

At this point, the study of "what algorithms to solve before Python programming interview" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report