“
每
日
一
練
”
Aug.
19
Data Application Lab 自2017年6月15日起,每天和你分享討論一道數據科學(DS)和商業分析(BA)領域常見的面試問題。
自2017年10月4日起,每天再為大家分享一道Leetcode 算法題。
希望積極尋求相關領域工作的你每天關注我們的問題并且與我們一起思考,我們將會在第二天給出答案。
Day
579
DS Interview Question
What’s the disadvantages of linear regression?
BA Interview Question
Human Traffic of Stadium
X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people
Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).
For example, the table stadium:
+------+------------+-----------+
| id | date | people |
+------+------------+-----------+
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
+------+------------+-----------+
For the sample data above, the output is:
+------+------------+-----------+
| id | date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-08 | 188 |
+------+------------+-----------+
LeetCode Question
Remove Element
Deion:
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Input: [3,2,2,3]
Output: 2
Assumptions:
Do not allocate extra space for another array, you must do this in place with constant memory.
Day
578
答案揭曉
DS Interview Question & Answer
If I double every sample observation in a linear regression model, how will the coefficients, r-squared value and t-value change?
Answer:
The coefficients will be the same (for the analytical solution will not be affected).
R-squared value will be the same (please refer to the definition of R-squared value).
T-value will be roughly sqrt(2) times the previous t value:
Reference:
https://stats.stackexchange.com/questions/19698/if-i-repeat-every-sample-observation-in-a-linear-regression-model-and-rerun-the
http://reliawiki.org/index.php/Simple_Linear_Regression_Analysis
https://en.wikipedia.org/wiki/Coefficient_of_determination
https://en.wikipedia.org/wiki/Student%27s_t-test
BA Interview Question & Answer
Classes More Than 5 Students
There is a table courses with columns: student and class
Please list out all classes which have more than or equal to 5 students.
For example, the table:
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+
Should output:
+---------+
| class |
+---------+
| Math |
+---------+
Answer:
Approach: Using GROUP BY clause and sub-query [Accepted]
Intuition
First, we can count the student number in each class. And then select the ones have more than 5 students.
Algorithm
To get the student number in each class. We can use GROUP BY and COUNT, which is very popular used to statistic bases on some character in a table.
SELECT
class, COUNT(DISTINCT student)
FROM
courses
GROUP BY class
;
Note: We use DISTINCT here since the student name may duplicated in a class as it is mentioned int he problem deion.
| class | COUNT(student) |
|----------|----------------|
| Biology | 1 |
| Computer | 1 |
| English | 1 |
| Math | 6 |
To continue, we can filter the classes by taking the above query as a sub-query.
SELECT
class
FROM
(SELECT
class, COUNT(DISTINCT student) AS num
FROM
courses
GROUP BY class) AS temp_table
WHERE
num >= 5
;
Note: Make an alias of COUNT(student) ('num' in this case) so that you can use in the WHERE clause because it cannot be used directly over there.
Approach: Using GROUP BY and HAVING condition [Accepted]
Algorithm
Using sub-query is one way to add some condition to a GROUP BY clause, however, using HAVING is another simpler and natural approach. So we can rewrite the above solution as below.
MySQL
SELECT
class
FROM
courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5
;
Reference: https://leetcode.com/problems/classes-more-than-5-students/deion/
LeetCode Question & Answer
Remove Duplicate
Deion:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length
Do not allocate extra space for another array, you must do this in place with constant memory.
Input: [1,1,2]
Output: 2
Assumptions:
Do not allocate extra space for another array, you must do this in place with constant memory.
Solution:這是一道非常基礎的去重題目,需要細心設定兩個指針并考慮好邊界條件。
Code:
時間復雜度:O(n)
空間復雜度:O(1)
責任編輯: