"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
"SELECT
i.item_category,
o.order_date,
SUM(o.orderquantity) AS totalunits_ordered
FROM
orders o
JOIN
items i ON o.itemid = i.itemid
WHERE
o.order_date >= DATE('now', '-6 days')
GROUP BY
i.item_category,
o.order_date
ORDER BY
i.item_category ASC,
o.order_date ASC;"
Anonymous Tortoise - "SELECT
i.item_category,
o.order_date,
SUM(o.orderquantity) AS totalunits_ordered
FROM
orders o
JOIN
items i ON o.itemid = i.itemid
WHERE
o.order_date >= DATE('now', '-6 days')
GROUP BY
i.item_category,
o.order_date
ORDER BY
i.item_category ASC,
o.order_date ASC;"See full answer
"I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you!
def encrypt(word):
en_word = ""
for i in range(len(word)):
if i == 0:
en_word += chr(ord(word[0])+1)
else:
num = ord(word[i]) + ord(en_word[i-1])
while num > 122:
num -= 26
en_word += chr(num)
return en_word
def decrypt(word):
de_word = ""
for i in range(len(word)):
if i == 0:
de_word += chr(ord(word[i]"
Anonymous Armadillo - "I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you!
def encrypt(word):
en_word = ""
for i in range(len(word)):
if i == 0:
en_word += chr(ord(word[0])+1)
else:
num = ord(word[i]) + ord(en_word[i-1])
while num > 122:
num -= 26
en_word += chr(num)
return en_word
def decrypt(word):
de_word = ""
for i in range(len(word)):
if i == 0:
de_word += chr(ord(word[i]"See full answer
"def getcheapestcost(rootNode):
\# need to do DFS for each branch
\# but this can be done recursively
n = len(rootNode.children)
if n == 0:
return 0
else:
min_cost = float('inf')
for i in range(len(n)):
tempcost = getcheapest_cost(rootNode.children[i])
if (tempcost < mincost):
mincost = tempcost
return min_cost + rootNode.cost
\# A node
class Node:
\# Constructor to create a new node
def init\(self, cost):
self.cost = cost
self.children = []
self.parent = None"
Anonymous Owl - "def getcheapestcost(rootNode):
\# need to do DFS for each branch
\# but this can be done recursively
n = len(rootNode.children)
if n == 0:
return 0
else:
min_cost = float('inf')
for i in range(len(n)):
tempcost = getcheapest_cost(rootNode.children[i])
if (tempcost < mincost):
mincost = tempcost
return min_cost + rootNode.cost
\# A node
class Node:
\# Constructor to create a new node
def init\(self, cost):
self.cost = cost
self.children = []
self.parent = None"See full answer
"Test case is wrong. It expects to sort in asc order of month_year.
-- Write your query here
SELECT
strftime('%Y-%m', createdat) AS monthyear,
COUNT(DISTINCT userid) AS numcustomers,
COUNT(t.id) AS num_orders,
SUM(price * quantity) AS order_amt
FROM
transactions t
INNER JOIN products p
ON t.product_id = p.id
GROUP BY
month_year
ORDER BY
month_year
;
"
Aneesha K. - "Test case is wrong. It expects to sort in asc order of month_year.
-- Write your query here
SELECT
strftime('%Y-%m', createdat) AS monthyear,
COUNT(DISTINCT userid) AS numcustomers,
COUNT(t.id) AS num_orders,
SUM(price * quantity) AS order_amt
FROM
transactions t
INNER JOIN products p
ON t.product_id = p.id
GROUP BY
month_year
ORDER BY
month_year
;
"See full answer
"Hi, my solution gives the exact numerical values as the proposed solution, but it doesn't pass the tests. Am I missing something, or is this a bug?
def findrevenueby_city(transactions: pd.DataFrame,
users: pd.DataFrame,
exchange_rate: pd.DataFrame) -> pd.DataFrame:
gets user city for each user id
userids = users[['id', 'usercity']]
and merge on transactions
transactions = transactions.merge(user_ids, how='left"
Gabriel P. - "Hi, my solution gives the exact numerical values as the proposed solution, but it doesn't pass the tests. Am I missing something, or is this a bug?
def findrevenueby_city(transactions: pd.DataFrame,
users: pd.DataFrame,
exchange_rate: pd.DataFrame) -> pd.DataFrame:
gets user city for each user id
userids = users[['id', 'usercity']]
and merge on transactions
transactions = transactions.merge(user_ids, how='left"See full answer
"Without using the window function:
SELECT w1.viewer_id
FROM watch_time w1
JOIN watch_time w2
ON abs(w1.month - w2.month) = 1
AND w1.viewerid = w2.viewerid
AND w1.year = w2.year
GROUP BY w1.viewer_id
HAVING sum(CASE
WHEN w1.month > w2.month
AND w1.watchhours > w2.watchhours THEN 1
ELSE 0
END) > 2
`"
Pk - "Without using the window function:
SELECT w1.viewer_id
FROM watch_time w1
JOIN watch_time w2
ON abs(w1.month - w2.month) = 1
AND w1.viewerid = w2.viewerid
AND w1.year = w2.year
GROUP BY w1.viewer_id
HAVING sum(CASE
WHEN w1.month > w2.month
AND w1.watchhours > w2.watchhours THEN 1
ELSE 0
END) > 2
`"See full answer
"Schema is wrong - id from product is mapped to id from transactions, id from product should point to product_id in transcations table"
Arshad P. - "Schema is wrong - id from product is mapped to id from transactions, id from product should point to product_id in transcations table"See full answer
"This problem can be solved with two approaches
Iterative approach
Recursive approach
Quite easy to think about the iterative approach, you can make use of a while loop in that case. But what if you want to make use of previously computed values? That case going for the recursive solution is quite useful.
class Collatz:
def init(self) -> None:
self.cache = {}
self.steps = 0
def steps_from(self, n) -> int:
# base case
if n == 1:
"
Frederick A. - "This problem can be solved with two approaches
Iterative approach
Recursive approach
Quite easy to think about the iterative approach, you can make use of a while loop in that case. But what if you want to make use of previously computed values? That case going for the recursive solution is quite useful.
class Collatz:
def init(self) -> None:
self.cache = {}
self.steps = 0
def steps_from(self, n) -> int:
# base case
if n == 1:
"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
"We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria).
To develop a function to set this up.
Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"
Prashanth A. - "We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria).
To develop a function to set this up.
Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"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
"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