Leetcode
Leetcode
Two Sum:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
for i in range(n):
x = target - nums[i]
if x in numdict:
return [numdict[x], i]
numdict[nums[i]] = i
return []
2.Roman to Integer:
Input: s = "LVIII"
Output: 58
for i in range(n):
if i < n-1 and dictnum[list1[i]] < dictnum[list1[i+1]]:
ans = ans - dictnum[list1[i]]
else:
ans = ans + dictnum[list1[i]]
return ans
ans = ''
4.Valid Parentheses:
Input: s = "()[]{}"
Output: true
fl
fl
fi
fl
fl
def isValid(self, s: str) -> bool:
list1 = []
for i in s:
if i in '([{':
list1.append(i)
else:
if
not list1 or \
(i
== ')' and list1[-1] != '(') or \
(i
== ']' and list1[-1] != '[') or \
(i
== '}' and list1[-1] != '{') :
return False
list1.pop()
return not list1
6.Remove Element
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
10.Plus One:
Input: digits = [1,2,3]
Output: [1,2,4]
for i in prices[1:]:
max_p = max(max_p, i-min_p)
min_p = min(min_p, i)
return max_p
12.Valid Palindrome:
Input: s = "A man, a plan, a canal: Panama"
Output: trueExplana on: "amanaplanacanalpanama" is a palindrome.
13.Single Number:
Given a non-empty array of integers nums, every element appears twice except for one. Find that
single one.
Input: nums = [2,2,1]
Output: 1
fi
ti
ti
def singleNumber(self, nums: List[int]) -> int:
dictcnt = Counter(nums)
return [i for i in dictcnt.keys() if dictcnt[i] == 1][0]
14.Majority Element:
The majority element is the element that appears more than ⌊n / 2⌋ mes. You may assume that the
majority element always exists in the array.
Input: nums = [2,2,1,1,1,2,2]
Output: 2
15.Happy Number:
Input: n = 19Output: true
Explana on:
12 + 92 = 82 (2s are square)
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
16.Isomorphic Strings:
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
Input: s = "egg", t = "add"
Output: true
for i in s:
li1.append(s.index(i))
for j in t:
li2.append(t.index(j))
if li1 == li2:
return True
else:
False
ti
ti
17.Contains Duplicate:
Given an integer array nums, return true if any value appears at least twice in the array, and
return false if every element is dis nct.
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
19.Summary Ranges:
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explana on: The ranges are:
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
21.Missing Number:
Input: nums = [3,0,1]
Output: 2
Explana on: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing
number in the range since it does not appear in nums.
22.Move Zeroes:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
23.Word Pa ern:
Input: pa ern = "abba", s = "dog cat cat sh"
Output: false
24.Reverse String:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
28.Ransom Note:
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using
the le ers from magazine and false otherwise.
Each le er in magazine can only be used once in ransomNote.
Input: ransomNote = "aa", magazine = "aab"
Output: true
tt
tt
ti
ti
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
dict_ran = Counter(ransomNote)
dict_mag = Counter(magazine)
for i in dict_ran.keys():
if dict_ran[i] <= dict_mag[i]:
pass
else:
return False
return True
31.Is Subsequence:
A subsequence of a string is a new string that is formed from the original string by dele ng some (can
be none) of the characters without disturbing the rela ve posi ons of the remaining characters.
(i.e., "ace" is a subsequence of "abcde" while "aec" is not).
Input: s = "abc", t = "ahbgdc"
Output: true
if j == len(li):
return True
return False
ti
ff
tt
tt
ffl
ti
ti
tt
ti
ti
32.Longest Palindrome:
Given a string s which consists of lowercase or uppercase le ers, return the length of the longest
palindrome that can be built with those le ers.
Input: s = "abccccdd"
Output: 7
Explana on: One longest palindrome that can be built is "dccaccd", whose length is 7.
if odd:
a = sum(odd)-len(odd)+1
return a + sum(even)
else:
return sum(even)
35.Assign Cookies:
Input: g = [1,2], s = [1,2,3]
Output: 2
Explana on: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gra fy all of the children,
You need to output 2.
res = res.replace('-','',1)
return res
stack.append(nums2[0])
for i in range(1,len(nums2)):
while stack and nums2[i] > stack[-1]:
dict_i[stack[-1]] = nums2[i]
stack.pop()
stack.append(nums2[i])
for i in stack:
dict_i[i] = -1
for i in nums1:
res.append(dict_i[i])
return res
40.Keyboard Row:
Given an array of strings words, return the words that can be typed using le ers of the alphabet on
only one row of American keyboard like the image below.
Input: words = ["Hello","Alaska","Dad","Peace"]
Output: ["Alaska","Dad"]
41.Rela ve Ranks:
Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Explana on: The placements are [1st, 5th, 3rd, 2nd, 4th].
for i in score:
res.append(dict_rnk[i])
return res
42.Detect Capital:
Input: word = "FlaG"
Output: false
45.Distribute Candies:
Alice has n candies, where the ith candy is of type candyType[i]. Alice no ced that she started to gain
weight, so she visited a doctor.
The doctor advised Alice to only eat n / 2 of the candies she has (n is always even). Alice likes her
candies very much, and she wants to eat the maximum number of di erent types of candies while s ll
following the doctor's advice.
Given the integer array candyType of length n, return the maximum number of di erent types of
candies she can eat if she only eats n / 2 of them.
Input: candyType = [1,1,2,2,3,3]
Output: 3
Explana on: Alice can only eat 6 / 2 = 3 candies. Since there are only 3 types, she can eat one of each
type.
if a<=len(set_1):
return a
else:
return len(set_1)
return res
for i in li:
if list1.index(i)+list2.index(i) == a:
res.append(i)
return res
for i in range(len(flowerbed)):
if flowerbed[i] == 0 and (i ==0 or flowerbed[i-1]==0) and
(i==len(flowerbed)-1 or flowerbed[i+1]==0):
flowerbed[i] = 1
n -= 1
if n == 0:
return True
return False
50.Set Mismatch:
Find the number that occurs twice and the number that is missing and return them in the form of an
array.
Input: nums = [1,2,2,4]
Output: [2,3]
fl
ti
fl
fl
fl
fl
fl
fl
def findErrorNums(self, nums: List[int]) -> List[int]:
dict_cnt = Counter(nums)
li = list(range(1,len(nums)+1))
res = []
for i in dict_cnt.keys():
if dict_cnt[i] == 2:
res.append(i)
res.append(list(set(li)-set(nums))[0])
return res
51.Degree of an Array:
Input: nums = [1,2,2,3,1,4,2]
Output: 6
Explana on:
The degree is 3 because the element 2 is repeated 3 mes.
So [2,2,3,1,4,2] is the shortest subarray, therefore returning 6.
Write a SQL query to get the second highest salary from the Employee table.
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 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 |
+---------------------+
SELECT
ti
ti
(SELECT distinct Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary
import pandas as pd
if len(sorted) < 2:
return pd.DataFrame({'SecondHighestSalary': [None]})
second_highest = sorted.iloc[1]
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same
ranking. Note that after a tie, the next ranking number should be the next consecutive integer value.
In other words, there should be no "holes" between ranks.
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
For example, given the above Scores table, your query should generate the following report (order
by highest score):
+-------+---------+
| score | Rank |
+-------+---------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+---------+
Important Note: For MySQL solutions, to escape reserved words used as column names, you can
use an apostrophe before and after the keyword. For example Rank.
Table: Logs
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| num | varchar |
+-------------+---------+
id is the primary key for this table.
Write an SQL query to find all numbers that appear at least three times consecutively.
Logs table:
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Result table:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
1 is the only number that appears consecutively for at least three times.
with cte as (
select num,
lead(num,1) over() as next,
lead(num,2) over() as next_2
from Logs
)
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL query to find employees who have the highest salary in each of the departments. For the
above tables, your SQL query should return the following rows (order of rows does not matter).
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
import pandas as pd
Write an SQL query that reports the fraction of players that logged in again on the day after the day
they first logged in, rounded to 2 decimal places. In other words, you need to count the number of
players that logged in for at least two consecutive days starting from their first login date, then divide
that number by the total number of players.
Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 2 | 2016-03-02 | 6 |
| 2 | 3 | 2017-06-25 | 1 |
| 3 | 1 | 2016-03-02 | 0 |
| 3 | 4 | 2018-07-03 | 5 |
+-----------+-----------+------------+--------------+
Result table:
+-----------+
| fraction |
+-----------+
| 0.33 |
+-----------+
Only the player with id 1 logged back in after the first day he had logged
in so the answer is 1/3 = 0.33
The Employee table holds all employees including their managers. Every employee has an Id, and
there is also a column for the manager Id.
+------+----------+-----------+----------+
|Id |Name |Department |ManagerId |
+------+----------+-----------+----------+
|101 |John |A |null |
|102 |Dan |A |101 |
|103 |James |A |101 |
|104 |Amy |A |101 |
|105 |Anne |A |101 |
|106 |Ron |B |101 |
+------+----------+-----------+----------+
Given the Employee table, write a SQL query that finds out managers with at least 5 direct report.
For the above table, your SQL query should return:
+-------+
| Name |
+-------+
| John |
+-------+
import pandas as pd
return manager[['name']]
Write a query to print the sum of all total investment values in 2016 (TIV_2016), to a scale of 2
decimal places, for all policy holders who meet the following criteria:
where PID is the policyholder’s policy ID, TIV_2015 is the total investment value in 2015, TIV_2016 is
the total investment value in 2016, LAT is the latitude of the policy holder’s city, and LON is the
longitude of the policy holder’s city.
Sample Input
Sample Output
| TIV_2016 |
|----------|
| 45.00 |
import pandas as pd
In social network like Facebook or Twitter, people send friend requests and accept others’ requests as
well. Table request_accepted holds the data of friend acceptance, while requester_id and
accepter_id both are the id of a person.
Write a query to find the the people who has most friends and the most friends number. For the
sample data above, the result is:
| id | num |
|----|-----|
| 3 | 3 |
Note:
It is guaranteed there is only 1 people having the most friends. The friend request could only been
accepted once, which mean there is no multiple records with the same requester_id and accepter_id
value. Explanation: The person with id ‘3’ is a friend of people ‘1’, ‘2’ and ‘4’, so he has 3 friends in
total, which is the most number than any others.
Follow-up: In the real world, multiple people could have the same most number of friends, can you
find all these people in this case?
with cte as (
select requester_id as id from RequestAccepted
union all
select accepter_id as id from RequestAccepted
)
import pandas as pd
Mary is a teacher in a middle school and she has a table seat storing students' names and their
corresponding seat ids.
Can you write a SQL query to output the result for Mary?
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
select case
when id = (select max(id) from Seat) and id%2 = 1
then id
when id%2 = 1
then id+1
else
id-1
end as id,
student from Seat
order by id
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key column for this table.
Write an SQL query for a report that provides the customer ids from the Customer table that bought
all the products in the Product table.
For example:
Customer table:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
+-------------+-------------+
Product table:
+-------------+
| product_key |
+-------------+
| 5 |
| 6 |
+-------------+
Result table:
+-------------+
| customer_id |
+-------------+
| 1 |
| 3 |
+-------------+
The customers who bought all the products (5 and 6) are customers with id 1
and 3.
import pandas as pd
df = customer.drop_duplicates(keep = 'first'
).groupby('customer_id').count().
reset_index()
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id is the primary key of this table.
Write an SQL query that selects the product id, year, quantity, and price for the first year of every
product sold.
Sales table:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+---------+------------+------+----------+-------+
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+
Result table:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100 | 2008 | 10 | 5000 |
| 200 | 2011 | 15 | 9000 |
+------------+------------+----------+-------+
import pandas as pd
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
+----------------+---------+
user_id is the primary key of this table.
This table has the info of the users of an online shopping website where
users can sell and buy items.
Table: Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
+---------------+---------+
order_id is the primary key of this table.
item_id is a foreign key to the Items table.
buyer_id and seller_id are foreign keys to the Users table.
Table: Items
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
item_id is the primary key of this table.
Write an SQL query to find for each user, the join date and the number of orders they made as a
buyer in 2019.
Users table:
+---------+------------+----------------+
| user_id | join_date | favorite_brand |
+---------+------------+----------------+
| 1 | 2018-01-01 | Lenovo |
| 2 | 2018-02-09 | Samsung |
| 3 | 2018-01-19 | LG |
| 4 | 2018-05-21 | HP |
+---------+------------+----------------+
Orders table:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1 | 2019-08-01 | 4 | 1 | 2 |
| 2 | 2018-08-02 | 2 | 1 | 3 |
| 3 | 2019-08-03 | 3 | 2 | 3 |
| 4 | 2018-08-04 | 1 | 4 | 2 |
| 5 | 2018-08-04 | 1 | 3 | 4 |
| 6 | 2019-08-05 | 2 | 2 | 4 |
+----------+------------+---------+----------+-----------+
Items table:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
+---------+------------+
Result table:
+-----------+------------+----------------+
| buyer_id | join_date | orders_in_2019 |
+-----------+------------+----------------+
| 1 | 2018-01-01 | 1 |
| 2 | 2018-02-09 | 2 |
| 3 | 2018-01-19 | 0 |
| 4 | 2018-05-21 | 0 |
+-----------+------------+----------------+
import pandas as pd
Table: Products
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| product_id | int |
| new_price | int |
| change_date | date |
+---------------+---------+
(product_id, change_date) is the primary key of this table.
Each row of this table indicates that the price of some product was changed
to a new price at some date.
Write an SQL query to find the prices of all products on 2019-08-16. Assume the price of all products
before any change is 10.
Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1 | 20 | 2019-08-14 |
| 2 | 50 | 2019-08-14 |
| 1 | 30 | 2019-08-15 |
| 1 | 35 | 2019-08-16 |
| 2 | 65 | 2019-08-17 |
| 3 | 20 | 2019-08-18 |
+------------+-----------+-------------+
Result table:
+------------+-------+
| product_id | price |
+------------+-------+
| 2 | 50 |
| 1 | 35 |
| 3 | 10 |
+------------+-------+
import pandas as pd
Table: Delivery
+-----------------------------+---------+
| Column Name | Type |
+-----------------------------+---------+
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
+-----------------------------+---------+
delivery_id is the primary key of this table.
The table holds information about food delivery to customers that make
orders at some date and specify a preferred delivery date (on the same order
date or after it).
If the preferred delivery date of the customer is the same as the order date then the order is called
immediate otherwise it's called scheduled.
The first order of a customer is the order with the earliest order date that customer made. It is
guaranteed that a customer has exactly one first order.
Write an SQL query to find the percentage of immediate orders in the first orders of all customers,
rounded to 2 decimal places.
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-02 |
| 2 | 2 | 2019-08-02 | 2019-08-02 |
| 3 | 1 | 2019-08-11 | 2019-08-12 |
| 4 | 3 | 2019-08-24 | 2019-08-24 |
| 5 | 3 | 2019-08-21 | 2019-08-22 |
| 6 | 2 | 2019-08-11 | 2019-08-13 |
| 7 | 4 | 2019-08-09 | 2019-08-09 |
+-------------+-------------+------------+-----------------------------+
Result table:
+----------------------+
| immediate_percentage |
+----------------------+
| 50.00 |
+----------------------+
The customer id 1 has a first order with delivery id 1 and it is scheduled.
The customer id 2 has a first order with delivery id 2 and it is immediate.
The customer id 3 has a first order with delivery id 5 and it is scheduled.
The customer id 4 has a first order with delivery id 7 and it is immediate.
Hence, half the customers have immediate first orders.
import pandas as pd
def immediate_food_delivery(delivery:
pd.DataFrame) -> pd.DataFrame:
den = len(df:=
delivery.groupby("customer_id").min())
# <-- 1
num = len(df[df.order_date ==
df.customer_pref_delivery_date])) #
<-- 2
return pd.DataFrame({"immediate_percentage":
[num / den *100]}).round(2)) # <-- 3
Table: Transactions
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+---------------+---------+
id is the primary key of this table.
The table has information about incoming transactions.
The state column is an enum of type ["approved", "declined"].
Write an SQL query to find for each month and country, the number of transactions and their total
amount, the number of approved transactions and their total amount.
The query result format is in the following example:
Transactions table:
+------+---------+----------+--------+------------+
| id | country | state | amount | trans_date |
+------+---------+----------+--------+------------+
| 121 | US | approved | 1000 | 2018-12-18 |
| 122 | US | declined | 2000 | 2018-12-19 |
| 123 | US | approved | 2000 | 2019-01-01 |
| 124 | DE | approved | 2000 | 2019-01-07 |
+------+---------+----------+--------+------------+
Result table:
+----------+---------+-------------+----------------+--------------------
+-----------------------+
| month | country | trans_count | approved_count | trans_total_amount |
approved_total_amount |
+----------+---------+-------------+----------------+--------------------
+-----------------------+
| 2018-12 | US | 2 | 1 | 3000 |
1000 |
| 2019-01 | US | 1 | 1 | 2000 |
2000 |
| 2019-01 | DE | 1 | 1 | 2000 |
2000 |
+----------+---------+-------------+----------------+--------------------
+-----------------------+
import pandas as pd
def monthly_transactions(transactions:
pd.DataFrame) -> pd.DataFrame:
transactions['month'] =
pd.to_datetime(transactions['trans_date']).dt.str
ftime('%Y-%m')
transactions['state'] =
transactions['state'].str.replace('approved',
'1').replace('declined', '0')
transactions['state'] =
transactions['state'].astype(int)
transactions['approved_total_amount'] =
transactions['amount'] * transactions['state']
resq = transactions.groupby(['month',
'country'], dropna=False).agg({'id' : 'count',
'state' : 'sum', 'amount' : 'sum',
'approved_total_amount' : 'sum'}).reset_index()
return resq.rename(columns={'id' :
'trans_count', 'state' : 'approved_count',
'amount' : 'trans_total_amount'})
Table: Queue
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| person_id | int |
| person_name | varchar |
| weight | int |
| turn | int |
+-------------+---------+
person_id is the primary key column for this table.
This table has the information about all people waiting for an elevator.
The person_id and turn columns will contain all numbers from 1 to n, where n
is the number of rows in the table.
Write an SQL query to find the person_name of the last person who will fit in the elevator without
exceeding the weight limit. It is guaranteed that the person who is first in the queue can fit in the
elevator.
Queue table
+-----------+-------------------+--------+------+
| person_id | person_name | weight | turn |
+-----------+-------------------+--------+------+
| 5 | George Washington | 250 | 1 |
| 3 | John Adams | 350 | 2 |
| 6 | Thomas Jefferson | 400 | 3 |
| 2 | Will Johnliams | 200 | 4 |
| 4 | Thomas Jefferson | 175 | 5 |
| 1 | James Elephant | 500 | 6 |
+-----------+-------------------+--------+------+
Result table
+-------------------+
| person_name |
+-------------------+
| Thomas Jefferson |
+-------------------+
Queue table is ordered by turn in the example for simplicity.
In the example George Washington(id 5), John Adams(id 3) and Thomas
Jefferson(id 6) will enter the elevator as their weight sum is 250 + 350 +
400 = 1000.
Thomas Jefferson(id 6) is the last person to fit in the elevator because he
has the last turn in these three people.
select person_name from (select person_name, turn, sum(weight)
over(order by turn) as a
from Queue) b
where a <=1000 order by turn desc limit 1
import pandas as pd
sort_queue = queue.sort_values(by='turn',
ascending=True)
sort_queue['Total Weight'] =
sort_queue['weight'].cumsum()
return sort_queue[sort_queue['Total Weight']
<= 1000][['person_name']].tail(1)
Table: Customer
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| customer_id | int |
| name | varchar |
| visited_on | date |
| amount | int |
+---------------+---------+
(customer_id, visited_on) is the primary key for this table.
This table contains data about customer transactions in a restaurant.
visited_on is the date on which the customer with ID (customer_id) have
visited the restaurant.
amount is the total paid by a customer.
You are the restaurant owner and you want to analyze a possible expansion (there will be at least one
customer every day).
Write an SQL query to compute moving average of how much customer paid in a 7 days window
(current day + 6 days before) .
average_amount should be rounded to 2 decimal places, all dates are in the format ('YYYY-MM-DD').
Customer table:
+-------------+--------------+--------------+-------------+
| customer_id | name | visited_on | amount |
+-------------+--------------+--------------+-------------+
| 1 | Jhon | 2019-01-01 | 100 |
| 2 | Daniel | 2019-01-02 | 110 |
| 3 | Jade | 2019-01-03 | 120 |
| 4 | Khaled | 2019-01-04 | 130 |
| 5 | Winston | 2019-01-05 | 110 |
| 6 | Elvis | 2019-01-06 | 140 |
| 7 | Anna | 2019-01-07 | 150 |
| 8 | Maria | 2019-01-08 | 80 |
| 9 | Jaze | 2019-01-09 | 110 |
| 1 | Jhon | 2019-01-10 | 130 |
| 3 | Jade | 2019-01-10 | 150 |
+-------------+--------------+--------------+-------------+
Result table:
+--------------+--------------+----------------+
| visited_on | amount | average_amount |
+--------------+--------------+----------------+
| 2019-01-07 | 860 | 122.86 |
| 2019-01-08 | 840 | 120 |
| 2019-01-09 | 840 | 120 |
| 2019-01-10 | 1000 | 142.86 |
+--------------+--------------+----------------+
select visited_on,
sum(amount) over (
order by visited_on
rows between 6 preceding AND 0 following
) as amount,
round(avg(amount) over (
order by visited_on
rows between 6 preceding AND 0 following
),2) as average_amount
from (
select visited_on, sum(amount) as amount
from Customer
group by visited_on
) a
order by visited_on
limit 18446744073709551610
offset 6
Table: Movies
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| title | varchar |
+---------------+---------+
movie_id is the primary key for this table.
title is the name of the movie.
Table: Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
+---------------+---------+
user_id is the primary key for this table.
Table: Movie_Rating
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| user_id | int |
| rating | int |
| created_at | date |
+---------------+---------+
(movie_id, user_id) is the primary key for this table.
This table contains the rating of a movie by a user in their review.
created_at is the user's review date.
Find the name of the user who has rated the greatest number of the movies.
In case of a tie, return lexicographically smaller user name.
•
•
Find the movie name with the highest average rating in February 2020.
In case of a tie, return lexicographically smaller movie name..
Query is returned in 2 rows, the query result format is in the following example:
Movies table:
+-------------+--------------+
| movie_id | title |
+-------------+--------------+
| 1 | Avengers |
| 2 | Frozen 2 |
| 3 | Joker |
+-------------+--------------+
Users table:
+-------------+--------------+
| user_id | name |
+-------------+--------------+
| 1 | Daniel |
| 2 | Monica |
| 3 | Maria |
| 4 | James |
+-------------+--------------+
Movie_Rating table:
+-------------+--------------+--------------+-------------+
| movie_id | user_id | rating | created_at |
+-------------+--------------+--------------+-------------+
| 1 | 1 | 3 | 2020-01-12 |
| 1 | 2 | 4 | 2020-02-11 |
| 1 | 3 | 2 | 2020-02-12 |
| 1 | 4 | 1 | 2020-01-01 |
| 2 | 1 | 5 | 2020-02-17 |
| 2 | 2 | 2 | 2020-02-01 |
| 2 | 3 | 2 | 2020-03-01 |
| 3 | 1 | 3 | 2020-02-22 |
| 3 | 2 | 4 | 2020-02-25 |
+-------------+--------------+--------------+-------------+
Result table:
+--------------+
| results |
+--------------+
| Daniel |
| Frozen 2 |
+--------------+
Daniel and Maria have rated 3 movies ("Avengers", "Frozen 2" and "Joker")
but Daniel is smaller lexicographically.
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is
smaller lexicographically.
import pandas as pd
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+---------------+---------+
(stock_name, day) is the primary key for this table.
The operation column is an ENUM of type ('Sell', 'Buy')
Each row of this table indicates that the stock which has stock_name had an
operation on the day operation_day with the price.
It is guaranteed that each 'Sell' operation for a stock has a corresponding
'Buy' operation in a previous day.
Write an SQL query to report the Capital gain/loss for each stock.
The capital gain/loss of a stock is total gain or loss after buying and selling the stock one or many
times.
Stocks table:
+---------------+-----------+---------------+--------+
| stock_name | operation | operation_day | price |
+---------------+-----------+---------------+--------+
| Leetcode | Buy | 1 | 1000 |
| Corona Masks | Buy | 2 | 10 |
| Leetcode | Sell | 5 | 9000 |
| Handbags | Buy | 17 | 30000 |
| Corona Masks | Sell | 3 | 1010 |
| Corona Masks | Buy | 4 | 1000 |
| Corona Masks | Sell | 5 | 500 |
| Corona Masks | Buy | 6 | 1000 |
| Handbags | Sell | 29 | 7000 |
| Corona Masks | Sell | 10 | 10000 |
+---------------+-----------+---------------+--------+
Result table:
+---------------+-------------------+
| stock_name | capital_gain_loss |
+---------------+-------------------+
| Corona Masks | 9500 |
| Leetcode | 8000 |
| Handbags | -23000 |
+---------------+-------------------+
Leetcode stock was bought at day 1 for 1000$ and was sold at day 5 for
9000$. Capital gain = 9000 - 1000 = 8000$.
Handbags stock was bought at day 17 for 30000$ and was sold at day 29 for
7000$. Capital loss = 7000 - 30000 = -23000$.
Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for
1010$. It was bought again at day 4 for 1000$ and was sold at day 5 for
500$. At last, it was bought at day 6 for 1000$ and was sold at day 10 for
10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy'
--> 'Sell') operation = (1010 - 10) + (500 - 1000) + (10000 - 1000) = 1000 -
500 + 9000 = 9500$.
select stock_name,
sum(case when operation = 'Buy' then -1*price
when operation = 'Sell' then price end) as capital_gain_loss
from Stocks
group by stock_name
import pandas as pd
Table: Accounts
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id | int |
| income | int |
+-------------+------+
account_id is the primary key (column with unique values) for this table.
Each row contains informa on about the monthly income for one bank account.
Write a solu on to calculate the number of bank accounts for each salary category. The salary
categories are:
"Low Salary": All the salaries strictly less than $20000.
"Average Salary": All the salaries in the inclusive range [$20000, $50000].
"High Salary": All the salaries strictly greater than $50000.
The result table must contain all three categories. If there are no accounts in a category, return 0.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Accounts table:
+------------+--------+
| account_id | income |
+------------+--------+
ti
ti
|3 | 108939 |
|2 | 12747 |
|8 | 87709 |
|6 | 91796 |
+------------+--------+
Output:
+----------------+----------------+
| category | accounts_count |
+----------------+----------------+
| Low Salary | 1 |
| Average Salary | 0 |
| High Salary | 3 |
+----------------+----------------+
Explana on:
Low Salary: Account 2.
Average Salary: No accounts.
High Salary: Accounts 3, 6, and 8.
import pandas as pd
ff=accounts[(accounts['income']>=20000)&(accounts['income']<=50
000)].shape[0]
ef=accounts[accounts['income']>50000].shape[0]
ti
result=pd.DataFrame({
'category':['High Salary','Low Salary','Average Salary'],
'accounts_count':[ef,df,ff]
})
return result