Chuẩn bị cho cuộc phỏng vấn về khoa học dữ liệu bằng cách nghiên cứu danh sách đầy đủ các câu hỏi Python kèm theo câu trả lời. Includes coding as well as theoretical questions
Data science is a well-paid and meaningful career. To maximize their chances of success, data scientists maintain a rich set of skills like Python, SQL, R, and other tools for working on data science projects
One way to improve your odds of landing a data scientist job is to have an in-demand data science technical skill like Python. It is one of the main programming languages used for data science, because it is simple, readable, and supports external libraries
Data scientists use Python for statistics and mathematical calculations. It is widely used for Machine Learning, specifically for writing Natural Language Processing and sentiment analysis algorithms. It’s also possible to write Python algorithms to analyze medical images and help detect and prevent fraud
Python is not the only language used for data science. R là một ngôn ngữ lập trình phổ biến khác để làm việc với dữ liệu. To learn about differences between Python and R, read our comparison article Python vs R for Data Science
Python is considered a simple programming language, but it has a lot of features to remember. Supporting libraries like pandas can further enhance its capabilities. We compiled a list of python data science interview questions to help you remember and practice everything you need to know before going into the interview
Basic Python Data Science Interview Questions
Let’s start with the simplest python data science interview questions. Người phỏng vấn sử dụng những câu hỏi này để đánh giá kiến thức của ứng viên về Python và khả năng viết mã của họ
Câu hỏi phỏng vấn khoa học dữ liệu Python không mã hóa
Writing Python code is important, but knowing its underlying principles is just as important. Usually interview questions are basic. For example, what are variables in Python and how to use them for data analysis
Let’s look at some questions asked of candidates during actual interviews
Question #1. Data structures in Python
This python data science interview question was asked during an interview with Walmart. The easy answer is to list data structures in Python. danh sách, bộ, tuple, từ điển
It’s great if you can describe the characteristics and features of each data structure and explain differences between them. For example, a list can contain many different types of values and can be mutated, whereas tuples are an immutable pair of two values
For a detailed answer, check out the ‘Solution Discussion’ section of this question on StrataScratch. Link is given above
Question #2. Difference between sorted[somelist] and somelist. sort[]
Link to the question. https. //platform. stratascratch. com/technical/2115-python-list
This question comes from the General Assembly, and it is a good indicator of a candidate's depth of knowledge of Python
The short answer is that the sorted[list] function results in a new list with sorted values, whereas somelist. sort[] sorts the original list in place, keeping its identity
Refer to the ‘Solution Discussion’ section of this question on StrataScratch platform to get a detailed breakdown of differences between two functions
Question #3. Memory Stored and Retrieved in Python’s Dictionary
Link to the question. https. //platform. stratascratch. com/technical/2091-python-dictionary
Dictionaries in Python store information as key-value pairs
person = { 'age': 24, 'firstName': 'George', 'lastName': 'Smith' }
We can retrieve a value of a specific key using square brackets - person[age], which would retrieve the value of ‘age’ key. Be prepared to answer similar questions. For example, to explain dictionary comprehension in Python
To better understand how Python dictionaries store data, check the ‘Solution Discussion’ section of this question on the StrataScratch platform
Question #4. Identifying Missing Values in a Data Set
Link to the question. https. //platform. stratascratch. com/technical/2254-missing-values-in-data-set
The easiest way to identify missing values is to use df. isnull[] or df. isna[] functions, available for Pandas dataframes. We can check the entire dataset using the df. isnull. values. any[] function, which returns true if there are missing values, and false if there are none
For a more detailed answer, check the ‘Solution Discussion’ section on StrataScratch platform
Question #5. Generate a sorted vector from two sorted vectors
Link to the question. https. //platform. stratascratch. com/technical/2104-sorted-vectors
Experience and knowledge of list comprehension in Python can help you easily answer this question
First, create a new array with the combined size of the first and second arrays. Then write a function that goes over arr_1 and arr_2 simultaneously, determines which is the smaller number from two arrays and adds it to the new array. And continues doing this until the end
You can look at the actual code on the ‘Solution Discussions’ section on StrataScratch platform
Coding Python Data Science Interview Questions
In the process of interviewing for a job, aspiring data scientists have to solve coding challenges as well. Bạn nên làm quen với việc viết mã Python
Follow these steps to easily solve even the most difficult questions
1. Understand the question
Before answering the question, make sure you completely understand it. For example, what columns need to be returned and other details for solving the question
When dealing with difficult questions, read them multiple times to wrap your head around them
Nếu bạn đã cố gắng hết sức để hiểu câu hỏi nhưng một số điều vẫn chưa rõ ràng, hãy hỏi người phỏng vấn để làm rõ
2. Analyze data
Looking at available table[s] can help you come up with a logical solution. Once you completely understand the verbal or written description of the question, look at the table and find columns that contain values that you’ll use in your calculations
To get the general idea of columns and values contained in them, look at types of values in each column. It will tell you if you need to convert values in certain columns
3. Lập kế hoạch tiếp cận của bạn
Khi bạn hoàn toàn hiểu cấu trúc của câu hỏi và dữ liệu có sẵn, bạn có thể bắt đầu giải nó. In theory, you can start writing Python code right away, but there’s a better approach
We recommend you to think about the problem in logical steps. Hãy nghĩ về tất cả các hoạt động và tính toán bạn cần thực hiện để đi từ dữ liệu có sẵn đến câu trả lời cuối cùng. Sau đó viết ra các bước hợp lý này và các chi tiết quan trọng cho từng bước
Python data science interview questions are usually very complex. One remedy is to organize your thoughts on paper. Bằng cách này, bạn sẽ thấy việc theo dõi và viết mã cho từng bước dễ dàng hơn nhiều
4. viết mã
Hãy suy nghĩ về những tính năng của Python mà giải pháp của bạn sẽ sử dụng. Nhập tất cả các thư viện khoa học dữ liệu cần thiết như numpy và python
Nếu bạn bối rối về việc xâu chuỗi nhiều hàm lại với nhau, hãy tạo các biến để lưu trữ kết quả của mỗi lệnh gọi hàm
Bắt đầu với một giải pháp cơ bản và dần dần thêm phức tạp vào nó. Xuất mã để xem các thay đổi gia tăng có tác dụng mong muốn không
Câu hỏi số 6. dự án đắt tiền
Hãy bắt đầu danh sách các thách thức mã hóa của chúng tôi với câu hỏi đơn giản này từ Microsoft. Như mọi khi, việc tìm ra câu trả lời đúng bắt nguồn từ việc chú ý đến dữ liệu và đặt ra các bước hợp lý để giải quyết câu hỏi
Liên kết đến câu hỏi. https. //nền tảng. tầng lớp. com/coding/10301-đắt-dự án
Hiểu câu hỏi
Để có câu trả lời, chúng ta phải tính phần trung bình của ngân sách phân bổ cho mỗi nhân viên
Câu hỏi yêu cầu chúng tôi xuất tên dự án, ngân sách cho mỗi khách hàng và sắp xếp các giá trị số theo thứ tự tăng dần
Phân tích dữ liệu
Không thực tế khi bắt đầu viết mã Python trước khi bạn hoàn toàn hiểu dữ liệu có sẵn
Trả lời câu hỏi này yêu cầu bạn phải làm việc với hai bảng - ms_projects chứa thông tin về các dự án và ms_emp_projects lưu trữ thông tin chi tiết của từng nhân viên
Trước tiên, hãy xem loại giá trị trong mỗi cột dữ liệu có sẵn
Nhìn vào dữ liệu thực tế trong bảng thậm chí có thể minh họa nhiều hơn và giúp bạn đưa điều này vào ngữ cảnh
ms_projects table
Table. ms_projects
idtitlebudget1Project1294982Project2324873Project3439094Project4157765Project536268
Hiển thị tất cảChuyển đổi dTypes
Cột id chứa các số để xác định từng dự án. Cột tiêu đề chứa tên của từng dự án và ngân sách chứa tổng ngân sách cho từng dự án
bảng ms_emp_projects
Bảng. ms_emp_projects
emp_idproject_id105921105932105943105954105965
Hiển thị tất cảChuyển đổi dTypes
Cột emp_id này chỉ đơn giản là một danh sách các số để xác định từng nhân viên và cột duy nhất khác được sử dụng để chỉ định dự án mà nhân viên đó được chỉ định
Lập kế hoạch tiếp cận của bạn
Để trả lời câu hỏi này, chúng ta cần tính tỷ lệ ngân sách trên nhân viên và sắp xếp danh sách cuối cùng theo thứ tự cụ thể
Vì thông tin về dự án và nhân viên được chia thành hai bảng nên chúng ta sẽ phải kết hợp chúng lại
Tiếp theo, chúng ta cần tìm số lượng công nhân cho mỗi dự án. Chúng tôi sử dụng nguyên tắc giống như trong SQL - tạo các nhóm nhân viên làm việc trong cùng một dự án, sau đó lấy số lượng nhân viên trong mỗi nhóm
Chúng ta có thể tính tỷ lệ ngân sách trên mỗi nhân viên bằng cách chia ngân sách cho số lượng nhân viên được chỉ định cho từng dự án
Cuối cùng, chúng ta cần sắp xếp các hàng sao cho những hàng có ngân sách cao nhất cho mỗi nhân viên được ưu tiên trước, theo sau là các giá trị thấp hơn
viết mã
Hãy dịch cách tiếp cận logic của chúng ta sang mã Python
Bước 1. Kết hợp dữ liệu từ hai bảng
Chúng tôi sử dụng chức năng hợp nhất [] từ thư viện Pandas, đây là giải pháp thay thế cho THAM GIA trong SQL. Nó cho phép chúng tôi kết hợp dữ liệu từ hai khung dữ liệu
Chúng tôi sử dụng từ khóa 'bật' để chỉ định thứ nguyên được chia sẻ giữa hai tập dữ liệu và từ khóa 'làm thế nào' để cho Python biết phải làm gì với các bản ghi không có giá trị khớp
import pandas as pd
import numpy as np
df=pd.merge[ms_projects, ms_emp_projects, how = 'inner',left_on = ['id'], right_on=['project_id']]
Bước 2. Tổng hợp số lượng công nhân cho mỗi dự án
In this step, we use the groupby[] function to create groups of employees working on different products. We use the size[] function to get a total number of employees in each group. This function in python works similar to the count[] aggregate function in SQL
df1=df.groupby[['title','budget']]['emp_id'].size[].reset_index[]
Once we have calculated the number of employees for each group, we use the reset_index[] to go back to default behavior. By default, each row in a dataframe is assigned a numeric index to identify it
Step 3. Calculate budget-per-employee ratio
We created a new property called ‘budget_emp_ratio’. We calculate values for this property by dividing the values in the budget column by the total number of employees
df1['budget_emp_ratio'] = [df1['budget']/df1['emp_id']].round[0]
Note that in the previous step, we aggregated values in the emp_id column for each group. So the emp_id column now contains a total number of employees
We also round the result of this division to the nearest whole number
Step 4. Output ordered values
Next, we use the sort_values[] function to arrange budget_emp_ratio values in a descending order [from highest to lowest], and output them as well as project names in the final result
df2=df1.sort_values[by='budget_emp_ratio',ascending=False]
result = df2[["title","budget_emp_ratio"]]
Here is what our final solution looks like
import pandas as pd
import numpy as np
df=pd.merge[ms_projects, ms_emp_projects, how = 'inner',left_on = ['id'], right_on=['project_id']]
df1=df.groupby[['title','budget']]['emp_id'].size[].reset_index[]
df1['budget_emp_ratio'] = [df1['budget']/df1['emp_id']].round[0]
df2=df1.sort_values[by='budget_emp_ratio',ascending=False]
result = df2[["title","budget_emp_ratio"]]
Output
The widget allows us to run our code, see the output and check if it’s the right answer
Our output exactly matches the expected output, so it is accepted as correct
Question #7. Total Cost Of Orders
A prime example of a Python data science interview question where a candidate has to use functions from pandas and numpy libraries to get the answer
Link to the question. https. //platform. stratascratch. com/coding/10183-total-cost-of-orders
Understand the question
We are asked to calculate the total of values in a certain column. Another condition is that we calculate the total for each different user
Question description tells us to output the id, first_name, and aggregate total_order_cost. It also specifies the way to order records
Phân tích dữ liệu
Let’s take a quick look at the types of columns in two tables. You might need to change the type of data, or use specific functions depending on type of data
The customers table contains typical information to describe each customer - their name, location, and contacts
Each record in the orders table stores information like a unique identifier for each order, identifier for customer who placed the order, order date, description, and cost
One important detail is to notice the cust_id column of the orders table. Values of this column reference the identity of each customer, stored in the id column of the customers table
Next, look at actual tables filled with data
bảng khách hàng
Table. customers
idfirst_namelast_namecityaddressphone_number8JohnJosephSan Francisco928-386-81647JillMichaelAustin813-297-06924WilliamDanielDenver813-368-12005HenryJacksonMiami808-601-751313EmmaIsaacMiami808-690-5201
Hiển thị tất cảChuyển đổi dTypes
orders table
Table. orders
idcust_idorder_dateorder_detailstotal_order_cost132019-03-04Coat100232019-03-01Shoes80332019-03-07Skirt30472019-02-01Coat25572019-03-10Shoes80
Hiển thị tất cảChuyển đổi dTypes
We must aggregate dollar values of each customer’s orders, stored in the total_order_cost column of the orders table
Lập kế hoạch tiếp cận của bạn
Solving questions like this one requires a thorough understanding of simple concepts like variables in Python, as well as knowledge of merge[], groupby[] and other functions from the pandas library
We need data from both tables to aggregate the value of orders placed by individual customers. Then we create a group of rows for each individual customer to store the information about orders placed by each order
Finally, we need aggregation Python functions to calculate the total of values in each group
Write the code
For more discussion about different approaches to the question, check out the question on our platform
Output
Mô tả câu hỏi cho biết đầu ra phải là ba cột này, với tên của khách hàng được sắp xếp theo thứ tự bảng chữ cái
Intermediate Python Data Science Interview Questions
Now let’s look at python data science interview questions that are a bit more complex. You might have to answer these if you’re interviewing for a middle data analyst or similar data scientist role
Question #8. Distances Traveled
This is a question from Lyft, where you have to find 10 users with highest distance values
Link to the question. https. //nền tảng. tầng lớp. com/coding/10324-quãng đường đã đi
Understand the question
Các điều kiện cho câu hỏi phỏng vấn khoa học dữ liệu python này khá rõ ràng. Chúng tôi cần tìm những người dùng có TOP 10 quãng đường đã đi và xuất ra ba cột sau. id, tên và khoảng cách
Phân tích dữ liệu
Trong câu hỏi này, dữ liệu có sẵn được lưu trữ trong hai bảng. Chúng ta cần làm việc với dữ liệu từ cả hai bảng để đi đến câu trả lời cuối cùng
Trước tiên, hãy xem loại giá trị trong mỗi cột. Đầu tiên, là bảng lyft_rides, chứa thông tin về các chuyến đi được đặt qua ứng dụng Lyft
Thứ hai là lyft_users, lưu trữ thông tin về người dùng ứng dụng
Cột user_id của bảng lyft_rides_log xác định khách hàng đã đặt chuyến đi. Một người dùng có thể đã đặt nhiều chuyến đi, vì vậy có thể an toàn khi cho rằng cột này có thể có các giá trị trùng lặp
Id người dùng và tên của họ được lưu trữ trong bảng lyft_users
Nó sẽ giúp xem xét dữ liệu thực tế trong hai bảng này
bảng lyft_rides_log
Bảng. lyft_rides_log
iduser_id distance101893102405610328831043383105187
Hiển thị tất cảChuyển đổi dTypes
bảng lyft_users
Bảng. lyft_users
idname1Dustin Smith2Jay Ramirez3Joseph Cooke4Melinda Young5Sean Parker
Hiển thị tất cảChuyển đổi dTypes
Nhìn vào hàng thứ năm trong bảng lyft_rides_log. Giá trị user_id của nó là 1, giá trị này chúng ta có thể tra cứu trong bảng lyft_users để biết thêm thông tin về người dùng. Trong trường hợp này, chúng tôi có thể tìm ra tên đầy đủ của họ, không có trong bảng lyft_rides_log
Lập kế hoạch tiếp cận của bạn
Chia nhỏ bất kỳ thử thách mã hóa nào thành một số bước dễ quản lý có thể giúp bạn tránh được lỗi
Để có được thông tin đầy đủ về người dùng và các chuyến đi mà họ đã đặt qua ứng dụng, chúng tôi sẽ phải kết hợp hai bảng thành một
Tiếp theo, chúng ta cần chia các chuyến đi thành các nhóm, một nhóm dành cho mỗi người dùng duy nhất đã đặt chuyến đi. Sau đó, chúng tôi có thể thêm khoảng cách cho mỗi người dùng
Khi chúng tôi tổng hợp khoảng cách cho từng người dùng, thì chúng tôi cần xếp hạng các nhóm dựa trên giá trị này
Bước cuối cùng sẽ là chọn người dùng có thứ hạng TOP 10 và trả về các cột đã chỉ định
Write the code
Để giải quyết câu hỏi này, chúng ta sẽ cần các hàm Python để kết hợp dữ liệu, nhóm các giá trị và xếp hạng các hàng dựa trên các giá trị trong một cột cụ thể
Bước 1. Kết hợp dữ liệu từ hai bảng
Chúng ta có thể sử dụng hàm merge[] để kết hợp dữ liệu từ hai bảng
import pandas as pd
import numpy as np
df = pd.merge[lyft_users, lyft_rides_log, left_on='id', right_on='user_id']
Bước 2. Tổng hợp số dặm đã đi và sắp xếp dữ liệu
Chúng ta có thể sử dụng hàm groupby[] để tạo các nhóm hàng dựa trên danh tính của người dùng
Chúng ta cũng cần hàm tổng hợp sum[] để cộng tất cả các giá trị khoảng cách cho mỗi nhóm
Sau đó, chúng tôi sử dụng sort_values[] để sắp xếp các giá trị theo thứ tự giảm dần
result = df.groupby[['user_id', 'name']]['distance'].sum[].to_frame[].sort_values[by = 'distance', ascending = False].reset_index[]
Chúng tôi sử dụng hàm to_frame[] để chuyển đổi các giá trị chuỗi thành khung dữ liệu. Điều này là cần thiết để xâu chuỗi hàm sort_values[] để sắp xếp các hàng
Bước 3. Xếp hạng các hàng dựa trên khoảng cách của chúng
Tiếp theo, chúng tôi tạo cột 'xếp hạng' và sử dụng hàm rank[] để tạo giá trị cho nó. Chúng tôi xác định thứ hạng dựa trên các giá trị trong cột 'khoảng cách' của mỗi hàng
________số 8_______Bước 4. Trả lại đầu ra cuối cùng
Chúng tôi đặt điều kiện để chọn các giá trị có 10 giá trị cao nhất trong cột khoảng cách và xuất các cột 'user_id', 'name' và 'khoảng cách'
result[result['rank']