每日一練 | Data Scientist & Business Analyst & Leetcode 面試題 609
“
每
日
一
練
”
May.
10
Data Application Lab 自2017年6月15日起,每天和你分享討論一道數據科學(DS)和商業分析(BA)領域常見的面試問題。
自2017年10月4日起,每天再爲大家分享一道Leetcode 算法題。
希望積極尋求相關領域工作的你每天關注我們的問題並且與我們一起思考,我們將會在第二天給出答案。
Day
509
DS Interview Question
What is A/B Testing?
BA Interview Question
Nth Highest Salary
Write a SQL query to get the nth highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
LeetCode Question
Pascal’s Triangle II
Deion:
Given an index k, return the kth row of the Pascal’s triangle.
Input: 3
Output: [1,3,3,1]
Assumptions:
Could you optimize your algorithm to use only O(k) extra space?
Day
508
答案揭曉
DS Interview Question & Answer
What's difference between pca and kernel pca?
Answer:
- The standard PCA always finds linear principal components to represent the data in lower dimension. Sometime, we need non-linear principal components.If we apply standard PCA for the below data, it will fail to find good representative direction. Kernel PCA (KPCA) rectifies this limitation.
- Kernel PCA just performs PCA in a new space.
- It uses Kernel trick to find principal components in different space (Possibly High Dimensional Space).
- PCA finds new directions based on covariance matrix of original variables. It can extract maximum P (number of features) eigen values. KPCA finds new directions based on kernel matrix. It can extract n (number of observations) eigenvalues.
- PCA allow us to reconstruct pre-image using few eigenvectors from total P eigenvectors. It may not be possible in KPCA.
- The computational complexity for KPCA to extract principal components take more time compared to Standard PCA.
https://www.quora.com/Whats-difference-between-pca-and-kernel-pca
BA Interview Question & Answer
Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |ta
| 2 | 200 |
| 3 | 300 |
+----+--------+
For example, given the above Employee table, the query should return 200 as the second highest salary. If there is no second highest salary, then the query should return null.
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
Answer:
1. Using sub-query and LIMIT clause
SELECT
(SELECT DISTINCT
Salary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary
;
OR
2. Using IFNULL and LIMIT clause:
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1),
NULL) AS SecondHighestSalary
Reference:
https://leetcode.com/problems/second-highest-salary/solution/
LeetCode Question & Answer
Pascal’s Triangle I
Deion:
Given numRows, generate the first numRows of Pascal’s triangle.
Input: 5
Output:
Solution:
根據圖上的規律來進行循環
注意防止數組越界
Code: