"bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"
Alvaro R. - "bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"See full answer
"int main()
{
int a1[7]={1,2,3,4,5,6,7};
int a2[7]={1,9,10,11,12,13,14};
vectorv;
v.insert(v.begin(),begin(a1),end(a1));
v.insert(v.begin(),begin(a2),end(a2));
int a3[v.size()];
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
a3[i]=v[i];
}
}
`"
Aryan D. - "int main()
{
int a1[7]={1,2,3,4,5,6,7};
int a2[7]={1,9,10,11,12,13,14};
vectorv;
v.insert(v.begin(),begin(a1),end(a1));
v.insert(v.begin(),begin(a2),end(a2));
int a3[v.size()];
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
a3[i]=v[i];
}
}
`"See full answer
"with base as (
select viewerid, year, month, watchhours,
lag(watchhours, 2) over (partition by viewerid order by year, month) as p3,
lag(watchhours, 1) over (partition by viewerid order by year, month) as p2
from watch_time
)
select
viewer_id
from base
where p3 < p2 and p2 < watch_hours
group by 1
`"
- Z. - "with base as (
select viewerid, year, month, watchhours,
lag(watchhours, 2) over (partition by viewerid order by year, month) as p3,
lag(watchhours, 1) over (partition by viewerid order by year, month) as p2
from watch_time
)
select
viewer_id
from base
where p3 < p2 and p2 < watch_hours
group by 1
`"See full answer
"WITH suspicious_transactions AS (
SELECT
c.first_name,
c.last_name,
t.receipt_number,
COUNT(t.receiptnumber) OVER (PARTITION BY c.customerid) AS noofoffences
FROM
customers c
JOIN
transactions t ON c.customerid = t.customerid
WHERE
t.receipt_number LIKE '%999%'
OR t.receipt_number LIKE '%1234%'
OR t.receipt_number LIKE '%XYZ%'
)
SELECT
first_name,
last_name,
receipt_number,
noofoffences
FROM
suspicious_transactions
WHERE
noofoffences >= 2;"
Jayveer S. - "WITH suspicious_transactions AS (
SELECT
c.first_name,
c.last_name,
t.receipt_number,
COUNT(t.receiptnumber) OVER (PARTITION BY c.customerid) AS noofoffences
FROM
customers c
JOIN
transactions t ON c.customerid = t.customerid
WHERE
t.receipt_number LIKE '%999%'
OR t.receipt_number LIKE '%1234%'
OR t.receipt_number LIKE '%XYZ%'
)
SELECT
first_name,
last_name,
receipt_number,
noofoffences
FROM
suspicious_transactions
WHERE
noofoffences >= 2;"See full answer
Data Scientist
Coding
+1 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"SELECT d.departmentname,SUM(o.orderamount) AS total_revenue FROM orders o
JOIN departments d
ON
d.departmentid =o.departmentid
WHERE o.orderdate >= CURRENTDATE - INTERVAL '12 months'
GROUP BY d.department_name
ORDER BY total_revenue DESC;
`"
Derrick M. - "SELECT d.departmentname,SUM(o.orderamount) AS total_revenue FROM orders o
JOIN departments d
ON
d.departmentid =o.departmentid
WHERE o.orderdate >= CURRENTDATE - INTERVAL '12 months'
GROUP BY d.department_name
ORDER BY total_revenue DESC;
`"See full answer
"SELECT upsellcampaignid, COUNT(DISTINCT trans.userid) AS eligibleusers
FROM campaign
JOIN "transaction" AS trans
ON transactiondate BETWEEN datestart AND date_end
JOIN user
ON trans.userid = user.userid
WHERE iseligibleforupsellcampaign = 1
GROUP BY upsellcampaignid
`"
Alina G. - "SELECT upsellcampaignid, COUNT(DISTINCT trans.userid) AS eligibleusers
FROM campaign
JOIN "transaction" AS trans
ON transactiondate BETWEEN datestart AND date_end
JOIN user
ON trans.userid = user.userid
WHERE iseligibleforupsellcampaign = 1
GROUP BY upsellcampaignid
`"See full answer
"
O(n) time, O(1) space
from typing import List
def maxsubarraysum(nums: List[int]) -> int:
if len(nums) == 0:
return 0
maxsum = currsum = nums[0]
for i in range(1, len(nums)):
currsum = max(currsum + nums[i], nums[i])
maxsum = max(currsum, max_sum)
return max_sum
debug your code below
print(maxsubarraysum([-1, 2, -3, 4]))
`"
Rick E. - "
O(n) time, O(1) space
from typing import List
def maxsubarraysum(nums: List[int]) -> int:
if len(nums) == 0:
return 0
maxsum = currsum = nums[0]
for i in range(1, len(nums)):
currsum = max(currsum + nums[i], nums[i])
maxsum = max(currsum, max_sum)
return max_sum
debug your code below
print(maxsubarraysum([-1, 2, -3, 4]))
`"See full answer
"select DISTINCT p.product_id,
p.product_name ,
CASE when sale_date is null then 'Not Sold'
else 'Sold'
END as sale_status
from products p
left join sales s
on p.productid= s.productid
`"
Gowtami K. - "select DISTINCT p.product_id,
p.product_name ,
CASE when sale_date is null then 'Not Sold'
else 'Sold'
END as sale_status
from products p
left join sales s
on p.productid= s.productid
`"See full answer
"SELECT DISTINCT title,
ROUND(AVG(rating) over (partition by title),1) avg_rating,
ROUND(AVG(rating) over (partition by genre),1) genre_rating
FROM rating r
JOIN movie m
ON r.movieid=m.movieid
ORDER by 1"
Harshi B. - "SELECT DISTINCT title,
ROUND(AVG(rating) over (partition by title),1) avg_rating,
ROUND(AVG(rating) over (partition by genre),1) genre_rating
FROM rating r
JOIN movie m
ON r.movieid=m.movieid
ORDER by 1"See full answer
"WITH CTE AS (
SELECT *, ROWNUMBER()OVER(PARTITION BY utxoid ORDER BY transactionid) AS trxrk
FROM transactions
JOIN transaction_inputs
USING (transaction_id)
JOIN utxo
USING (utxo_id)
)
SELECT transaction_id AS InvalidTransactionId
FROM CTE
WHERE sender!=address OR trx_rk > 1
`"
E L. - "WITH CTE AS (
SELECT *, ROWNUMBER()OVER(PARTITION BY utxoid ORDER BY transactionid) AS trxrk
FROM transactions
JOIN transaction_inputs
USING (transaction_id)
JOIN utxo
USING (utxo_id)
)
SELECT transaction_id AS InvalidTransactionId
FROM CTE
WHERE sender!=address OR trx_rk > 1
`"See full answer
"The solution produces the same result as the 'prescribed solution' yet it does not get accepted In the test results section
transcript['year'] = transcript['year'].astype(str)
df = pd.pivottable(data = transcript, index = 'studentid', columns = 'year', values = 'yearlygpa', aggfunc = 'mean').resetindex()
df = df[(df['2021'] < df['2022']) & (df['2022'] < df['2023'])]
df['average_gpa'] = df[['2021', '2022', '2023']].mean(axis=1).round(2)
return df
"
Prachi G. - "The solution produces the same result as the 'prescribed solution' yet it does not get accepted In the test results section
transcript['year'] = transcript['year'].astype(str)
df = pd.pivottable(data = transcript, index = 'studentid', columns = 'year', values = 'yearlygpa', aggfunc = 'mean').resetindex()
df = df[(df['2021'] < df['2022']) & (df['2022'] < df['2023'])]
df['average_gpa'] = df[['2021', '2022', '2023']].mean(axis=1).round(2)
return df
"See full answer
"this task is misleading . i used lag(1) and lead(1) cuz it did not say "compare temperature from 2 days before and 1 day before" , it reads to me as if its asking "compare cur temperature to prev and future and see if it rose and fall""
Erjan G. - "this task is misleading . i used lag(1) and lead(1) cuz it did not say "compare temperature from 2 days before and 1 day before" , it reads to me as if its asking "compare cur temperature to prev and future and see if it rose and fall""See full answer
"import random
def coin_flip():
x=4*[0]+[1]
res=[]
for i in range(20):
res.append(random.choice(x))
return res
res=[0,0] # [head,tail]
for j in range(1000):
temp=coin_flip()
res[0]+=sum(temp) #head
res[1]+=(20-sum(temp)) #tail"
Alireza K. - "import random
def coin_flip():
x=4*[0]+[1]
res=[]
for i in range(20):
res.append(random.choice(x))
return res
res=[0,0] # [head,tail]
for j in range(1000):
temp=coin_flip()
res[0]+=sum(temp) #head
res[1]+=(20-sum(temp)) #tail"See full answer
"with cte as
(select *,
row_number() over(order by score desc) as rn
from players)
select player_name, score, rn as ranking
from cte
where rn= 4 or rn =6 or rn =11
`"
Gowtami K. - "with cte as
(select *,
row_number() over(order by score desc) as rn
from players)
select player_name, score, rn as ranking
from cte
where rn= 4 or rn =6 or rn =11
`"See full answer
"SELECT
e1.empid AS manageremployee_id,
e1.empname AS managername,
COUNT(e2.empid) AS numberofdirectreports
FROM employees AS e1
INNER JOIN employees AS e2
ON e2.managerid = e1.empid
GROUP BY e1.emp_id
HAVING COUNT(e2.emp_id) >= 2
ORDER BY numberofdirectreports DESC, managername ASC
`"
Alvin P. - "SELECT
e1.empid AS manageremployee_id,
e1.empname AS managername,
COUNT(e2.empid) AS numberofdirectreports
FROM employees AS e1
INNER JOIN employees AS e2
ON e2.managerid = e1.empid
GROUP BY e1.emp_id
HAVING COUNT(e2.emp_id) >= 2
ORDER BY numberofdirectreports DESC, managername ASC
`"See full answer
"SELECT COUNT(DISTINCT o.customerid) AS customers, d.departmentname
FROM orders o
INNER JOIN departments d
ON d.departmentid = o.departmentid
WHERE d.departmentname IN ('Electronics','Fashion') AND o.orderdate BETWEEN '2022-01-01' AND '2022-12-31'
GROUP BY d.department_name;
`"
Derrick M. - "SELECT COUNT(DISTINCT o.customerid) AS customers, d.departmentname
FROM orders o
INNER JOIN departments d
ON d.departmentid = o.departmentid
WHERE d.departmentname IN ('Electronics','Fashion') AND o.orderdate BETWEEN '2022-01-01' AND '2022-12-31'
GROUP BY d.department_name;
`"See full answer