Facebook 面经题
Facebook
常考类型总结:
JOIN
CASE
update + join
replace + join
Building a histogram, 算某个量的分布,需要用到多个 GROUP BY
面经题
Q1
T1:user_id (showing today pays ads fee) on day T
T2:two columns, user_id and their status on day T-1
Status include: new, churn, resurrecting, existing
And ask you to use today’s user_id list to update T2
t是当天paid的fee,t-1是昨天的状态,分为几种情况,是否是新用户,还是回头客,还是旧用户。然后根据t的情况去更新t-1的table。用的是case when条件语句。
Note: 也可能是考 replace into, 如果有新的user出现在T
Q2
you have a table where you have date, user_id, song_id and count. It shows at the end of each day how many times in her history a user has listened to a given song. So count is cumulative sum. You have to update this on a daily basis based on a second table that records in real time when a user listens to a given song. Basically, at the end of each day, you go to this second table and pull a count of each user/song combination and then add this count to the first table that has the lifetime count. If it is the first time a user has listened to a given song, you won't have this pair in the lifetime table, so you have to create the pair there and then add the count of the last day.
Q3
There is a table that tracks every time a user turns a feature on or off, with columns user_id, action ("on" or "off), date, and time.
How many users turned the feature on today?
How many users have ever turned the feature on?
In a table that tracks the status of every user every day, how would you add today's data to it?
Q4
2 people listen to same music(at least 3 same music) find them out.
Solution: GROUP BY music, get the names of the music that 2 people are listening. Then find the 2 people listen to that music.
Q5
给一个post and comment table,找出comment数量的分布,用两个group by就可以了
Q6 leetcode 602. Friend Requests II: Who Has the Most Friends 升级版
Given an event-level table of interactions between pairs of users (note that there aren't duplicates in one day for one pair of users), for each possible number of "people interacted with" find the count for that group in a given day (i.e. 10 people interacted with only one person, 20 with 2, etc.).
Solution
Find the "union all" of the user 1 col and the user 2 col for one particular day. Group by user, and find the count for that user (user_count), this is how many people interacted with that user in that day. Finally, group this table by user_count and find a count by user_count. This solves the original question.
Q7
left outer join, inner join,如果inner join两个表以后行数变多了怎么回事,行数变少了怎么回事
Solution: 行数变多了是因为有link有重复值,行数变少了是因为link有NULL。
Q8
Write a sql query to find out the overall friend acceptance rate for a given date?
Table :- User_id_who_sent|User_id_to_whom|date|Action (Sent, accepted, rejected etc)
Q9
dialoglog
(userid int
appid int
eventID char , a flag either "imp" or "click"
timestamp
)
How to compute click-through rate (in mySQL)?
Now do it in for each app.
Q10
Write an SQL query that makes recommendations using the pages that your friends liked. Assume you have two tables: a two-column table of users and their friends, and a two-column table of users and the pages they liked. It should not recommend pages you already like.
Q11
how to generate all possible friendship given four tables with the "request", "accept","reject", and "remove" information.
Others:
Leetcode上Database里Customers Who Never Order/Find Cumulative Salary of an Employee/Customer Placing the Largest Number of Orders这几道题。
SQL assignment was to write a query which computes conversion rates for 2 products given the log of users actions with type of action for each product type.
Last updated