"def find_first(array: List[int], num: int) -> int:
lo = 0
hi = len(array)-1
while lo = num:
hi = mid - 1
if lo == mid and array[mid] == num:
return mid
else:
array[mid] < num
lo = mid + 1
return -1
`"
Gabriele G. - "def find_first(array: List[int], num: int) -> int:
lo = 0
hi = len(array)-1
while lo = num:
hi = mid - 1
if lo == mid and array[mid] == num:
return mid
else:
array[mid] < num
lo = mid + 1
return -1
`"See full answer
"Questions to ask :
Are there negative values in the input array? Interview : YES
Will the product of two number fit into 32-bit Integer. If not, will it fit 64-bit Integer. If not, then is it safe to use Big Integer? Interview : let's worry only about 32 bit Integer
What should we return if the input array is null or size (size of input array) is less than 2? Return 0
From above Information:
General approach is as follows :
a) Track smallest 2 elements in the array -> p"
Rajendra D. - "Questions to ask :
Are there negative values in the input array? Interview : YES
Will the product of two number fit into 32-bit Integer. If not, will it fit 64-bit Integer. If not, then is it safe to use Big Integer? Interview : let's worry only about 32 bit Integer
What should we return if the input array is null or size (size of input array) is less than 2? Return 0
From above Information:
General approach is as follows :
a) Track smallest 2 elements in the array -> p"See full answer
Coding
Data Structures & Algorithms
🧠Want an expert answer to a question? Saving questions lets us know what content to make next.
"Even more faster and vectorized version, using np.linalg.norm - to avoid loop and np.argpartition to select lowest k. We dont need to sort whole array - we need to be sure that first k elements are lower than the rest.
import numpy as np
def knn(Xtrain, ytrain, X_new, k):
distances = np.linalg.norm(Xtrain - Xnew, axis=1)
k_indices = np.argpartition(distances, k)[:k] # O(N) selection instead of O(N log N) sort
return int(np.sum(ytrain[kindices]) > k / 2.0)
`"
Dinar M. - "Even more faster and vectorized version, using np.linalg.norm - to avoid loop and np.argpartition to select lowest k. We dont need to sort whole array - we need to be sure that first k elements are lower than the rest.
import numpy as np
def knn(Xtrain, ytrain, X_new, k):
distances = np.linalg.norm(Xtrain - Xnew, axis=1)
k_indices = np.argpartition(distances, k)[:k] # O(N) selection instead of O(N log N) sort
return int(np.sum(ytrain[kindices]) > k / 2.0)
`"See full answer
"-- Write your query here
WITH high_value AS(
SELECT user_id
FROM user_sessions
JOIN attribution
ON usersessions.sessionid = attribution.session_id
GROUP BY user_id
HAVING SUM(purchase_value) > 100
ORDER BY SUM(purchase_value) DESC
)
SELECT usersessions.userid, marketing_channel
FROM user_sessions
JOIN high_value
ON usersessions.userid = highvalue.userid
JOIN attribution
ON usersessions.sessionid = attribution.session_id
GROUP BY usersessions.userid
H"
Alina G. - "-- Write your query here
WITH high_value AS(
SELECT user_id
FROM user_sessions
JOIN attribution
ON usersessions.sessionid = attribution.session_id
GROUP BY user_id
HAVING SUM(purchase_value) > 100
ORDER BY SUM(purchase_value) DESC
)
SELECT usersessions.userid, marketing_channel
FROM user_sessions
JOIN high_value
ON usersessions.userid = highvalue.userid
JOIN attribution
ON usersessions.sessionid = attribution.session_id
GROUP BY usersessions.userid
H"See full answer
"Use an index, two pointers, and a set to keep track of elements that you've seen.
pseudo code follows:
for i, elem in enumerate(array):
if elem in set return False
if i > N:
set.remove(array[i-N])"
Michael B. - "Use an index, two pointers, and a set to keep track of elements that you've seen.
pseudo code follows:
for i, elem in enumerate(array):
if elem in set return False
if i > N:
set.remove(array[i-N])"See full answer
"function findPrimes(n) {
if (n < 2) return [];
const primes = [];
for (let i=2; i <= n; i++) {
const half = Math.floor(i/2);
let isPrime = true;
for (let prime of primes) {
if (i % prime === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
}
return primes;
}
`"
Tiago R. - "function findPrimes(n) {
if (n < 2) return [];
const primes = [];
for (let i=2; i <= n; i++) {
const half = Math.floor(i/2);
let isPrime = true;
for (let prime of primes) {
if (i % prime === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
}
return primes;
}
`"See full answer
"
from typing import List
def getnumberof_islands(binaryMatrix: List[List[int]]) -> int:
if not binaryMatrix: return 0
rows = len(binaryMatrix)
cols = len(binaryMatrix[0])
islands = 0
for r in range(rows):
for c in range(cols):
if binaryMatrixr == 1:
islands += 1
dfs(binaryMatrix, r, c)
return islands
def dfs(grid, r, c):
if (
r = len(grid)
"
Rick E. - "
from typing import List
def getnumberof_islands(binaryMatrix: List[List[int]]) -> int:
if not binaryMatrix: return 0
rows = len(binaryMatrix)
cols = len(binaryMatrix[0])
islands = 0
for r in range(rows):
for c in range(cols):
if binaryMatrixr == 1:
islands += 1
dfs(binaryMatrix, r, c)
return islands
def dfs(grid, r, c):
if (
r = len(grid)
"See full answer
"from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"
Anonymous Roadrunner - "from typing import List
def three_sum(nums: List[int]) -> List[List[int]]:
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
l = i + 1
r = len(nums) - 1
while l 0:
r -= 1
elif potentialSum < 0:
l += 1
"See full answer
"
with youngsuccrate as(
select
strftime('%m', postdate) AS postmonth,
round(sum(issuccessfulpost)*1.0/count(issuccessfulpost),2)as yascrate
from
post
where
userid in (select userid from post_user where age between 0 and 18)
group by
post_month
),
nonyoungsucc_rate as(
select
strftime('%m', postdate) AS postmonth,
round(sum(issuccessfulpost)*1.0/count(issuccessfulpost),2)as nonyasc_rate
from
post
where
user_id in (select"
Bhavna S. - "
with youngsuccrate as(
select
strftime('%m', postdate) AS postmonth,
round(sum(issuccessfulpost)*1.0/count(issuccessfulpost),2)as yascrate
from
post
where
userid in (select userid from post_user where age between 0 and 18)
group by
post_month
),
nonyoungsucc_rate as(
select
strftime('%m', postdate) AS postmonth,
round(sum(issuccessfulpost)*1.0/count(issuccessfulpost),2)as nonyasc_rate
from
post
where
user_id in (select"See full answer
"Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"
Khushbu R. - "Let’s say the matrix is m x n (i.e., m rows and n columns).
Start from the top-right corner of the matrix.
Move left if you see a 1.
Move down if you see a 0.
Keep track of the row index where you last saw the leftmost 1 — that row has the most 1s.
public class MaxOnesRow {
public static int rowWithMostOnes(int matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int maxRowIndex = -1;
int j = cols - 1; /"See full answer
"Problem: Given a modified binary tree, where each node also has a pointer to it's parent, find the first common ancestor of two nodes.
Answer: As it happens, the structure that we're looking at is actually a linked list (one pointer up), so the problem is identical to trying to find if two linked lists share a common node.
How this works is by stacking the two chains of nodes together so they're the same length.
chain1 = node1
chain2= node2
while True:
chain1 = chain1.next
chain2=chain"
Michael B. - "Problem: Given a modified binary tree, where each node also has a pointer to it's parent, find the first common ancestor of two nodes.
Answer: As it happens, the structure that we're looking at is actually a linked list (one pointer up), so the problem is identical to trying to find if two linked lists share a common node.
How this works is by stacking the two chains of nodes together so they're the same length.
chain1 = node1
chain2= node2
while True:
chain1 = chain1.next
chain2=chain"See full answer
"I'm pretty sure Exponent's answer is wrong.
In the snippet below, they use "pl.name = 'Telephones' to attempt to filter down to the Telephone transactions, but they do this within a LEFT JOIN which means all product_lines rows are returned.
> LEFT JOIN product_lines pl
> ON p.productlineid = pl.id
> AND pl.name = 'Telephones'
Below is my solution. Also, I didn't see anywhere that said the "amount" column was in cents instead of dollars, but I still divided by 100 to be consistent with Exp"
Bradley E. - "I'm pretty sure Exponent's answer is wrong.
In the snippet below, they use "pl.name = 'Telephones' to attempt to filter down to the Telephone transactions, but they do this within a LEFT JOIN which means all product_lines rows are returned.
> LEFT JOIN product_lines pl
> ON p.productlineid = pl.id
> AND pl.name = 'Telephones'
Below is my solution. Also, I didn't see anywhere that said the "amount" column was in cents instead of dollars, but I still divided by 100 to be consistent with Exp"See full answer
"I might be missing something but the solution, seems to be incorrect.
...
, post_pairings AS (
SELECT
ps.user_id,
ps.postseqid AS failpostid,
ps.postseqid + 1 AS nextpostid
FROM post_seq AS ps
WHERE ps.issuccessfulpost IS TRUE
)
-- here ps.issuccessfulpost IS TRUE the condition should be FALSE
-- in that way ps.postseqid is the actual failed post(failpostid)
-- Additionally, at the end the join is assumming that the sequence id is going to match the post_id, wh"
Jaime A. - "I might be missing something but the solution, seems to be incorrect.
...
, post_pairings AS (
SELECT
ps.user_id,
ps.postseqid AS failpostid,
ps.postseqid + 1 AS nextpostid
FROM post_seq AS ps
WHERE ps.issuccessfulpost IS TRUE
)
-- here ps.issuccessfulpost IS TRUE the condition should be FALSE
-- in that way ps.postseqid is the actual failed post(failpostid)
-- Additionally, at the end the join is assumming that the sequence id is going to match the post_id, wh"See full answer