"from collections import deque
def updateword(words, startword, end_word):
if end_word not in words:
return None # Early exit if end_word is not in the dictionary
queue = deque([(start_word, 0)]) # (word, steps)
visited = set([start_word]) # Keep track of visited words
while queue:
word, steps = queue.popleft()
if word == end_word:
return steps # Found the target word, return steps
for i in range(len(word)):
"
叶 路. - "from collections import deque
def updateword(words, startword, end_word):
if end_word not in words:
return None # Early exit if end_word is not in the dictionary
queue = deque([(start_word, 0)]) # (word, steps)
visited = set([start_word]) # Keep track of visited words
while queue:
word, steps = queue.popleft()
if word == end_word:
return steps # Found the target word, return steps
for i in range(len(word)):
"See full answer
"SELECT employees.first_name,
managers.salary AS manager_salary
FROM employees
LEFT JOIN employees AS managers
ON employees.manager_id = managers.id
WHERE employees.salary > managers.salary
`"
Tiffany A. - "SELECT employees.first_name,
managers.salary AS manager_salary
FROM employees
LEFT JOIN employees AS managers
ON employees.manager_id = managers.id
WHERE employees.salary > managers.salary
`"See full answer
"Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach.
Iterative approach (JavaScript)
function reverseLL(head){
if(head === null) return head;
let prv = null;
let next = null;
let cur = head;
while(cur){
next = cur.next; //backup
cur.next = prv;
prv = cur;
cur = next;
}
head = prv;
return head;
}
Recursion Approach (JS)
function reverseLLByRecursion("
Satyam S. - "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach.
Iterative approach (JavaScript)
function reverseLL(head){
if(head === null) return head;
let prv = null;
let next = null;
let cur = head;
while(cur){
next = cur.next; //backup
cur.next = prv;
prv = cur;
cur = next;
}
head = prv;
return head;
}
Recursion Approach (JS)
function reverseLLByRecursion("See full answer
"public static boolean isPalindrome(String str){
boolean flag = true;
int len = str.length()-1;
int j = len;
for(int i=0;i<=len/2;i++){
if(str.charAt(i)!=str.charAt(j--)){
flag = false;
break;
}
}
return flag;
}"
Sravanthi M. - "public static boolean isPalindrome(String str){
boolean flag = true;
int len = str.length()-1;
int j = len;
for(int i=0;i<=len/2;i++){
if(str.charAt(i)!=str.charAt(j--)){
flag = false;
break;
}
}
return flag;
}"See full answer
Software Engineer
Data Structures & Algorithms
+4 more
🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.
"SELECT customer_id,
order_date,
orderid AS secondearliestorderid
FROM (
SELECT order_id,
customer_id,
order_date,
ROWNUMBER() OVER (PARTITION BY customerid, orderdate ORDER BY orderid ASC) AS rank
FROM orders
)
WHERE rank = 2
ORDER BY orderdate, customerid
`"
Tiffany A. - "SELECT customer_id,
order_date,
orderid AS secondearliestorderid
FROM (
SELECT order_id,
customer_id,
order_date,
ROWNUMBER() OVER (PARTITION BY customerid, orderdate ORDER BY orderid ASC) AS rank
FROM orders
)
WHERE rank = 2
ORDER BY orderdate, customerid
`"See full answer
"We can use dictionary to store cache items so that our read / write operations will be O(1).
Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1)
Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"
Alfred O. - "We can use dictionary to store cache items so that our read / write operations will be O(1).
Each time we read or update an existing record, we have to ensure the item is moved to the back of the cache. This will allow us to evict the first item in the cache whenever the cache is full and we need to add new records also making our eviction O(1)
Instead of normal dictionary, we will use ordered dictionary to store cache items. This will allow us to efficiently move items to back of the cache a"See full answer
"-- Write your query here
select
id,
(case
when p_id is null then 'Root'
when pid in (select id from treenode_table)
and id in (select pid from treenode_table) then 'Inner'
else 'Leaf' end) as node_types
from treenodetable
order by 1;
`"
Anonymous Roadrunner - "-- Write your query here
select
id,
(case
when p_id is null then 'Root'
when pid in (select id from treenode_table)
and id in (select pid from treenode_table) then 'Inner'
else 'Leaf' end) as node_types
from treenodetable
order by 1;
`"See full answer
"WITH filtered_posts AS (
SELECT
p.user_id,
p.issuccessfulpost
FROM
post p
WHERE
p.postdate >= '2023-11-01' AND p.postdate < '2023-12-01'
),
post_summary AS (
SELECT
pu.user_type,
COUNT(*) AS post_attempt,
SUM(CASE WHEN fp.issuccessfulpost = 1 THEN 1 ELSE 0 END) AS post_success
FROM
filtered_posts fp
JOIN
postuser pu ON fp.userid = pu.user_id
GROUP BY
pu.user_type
)
SELECT
user_type,
post_success,
post_attempt,
CAST(postsuccess AS FLOAT) / postattempt AS postsuccessrate
FROM
po"
David I. - "WITH filtered_posts AS (
SELECT
p.user_id,
p.issuccessfulpost
FROM
post p
WHERE
p.postdate >= '2023-11-01' AND p.postdate < '2023-12-01'
),
post_summary AS (
SELECT
pu.user_type,
COUNT(*) AS post_attempt,
SUM(CASE WHEN fp.issuccessfulpost = 1 THEN 1 ELSE 0 END) AS post_success
FROM
filtered_posts fp
JOIN
postuser pu ON fp.userid = pu.user_id
GROUP BY
pu.user_type
)
SELECT
user_type,
post_success,
post_attempt,
CAST(postsuccess AS FLOAT) / postattempt AS postsuccessrate
FROM
po"See full answer
"#include
#include
#include
using namespace std;
vector diff(const vector& A, const vector& B) {
unordered_set elements;
vector result;
for (const auto& element : A) {
elements.insert(element);
}
for (const auto& element : B) {
if (elements.find(element) == elements.end()) {
result.push_back(element);
} else {
elements.erase(element);
}
}
for"
Warrenbuff - "#include
#include
#include
using namespace std;
vector diff(const vector& A, const vector& B) {
unordered_set elements;
vector result;
for (const auto& element : A) {
elements.insert(element);
}
for (const auto& element : B) {
if (elements.find(element) == elements.end()) {
result.push_back(element);
} else {
elements.erase(element);
}
}
for"See full answer
"Here's a simpler solution:
select
u.username
, count(p.postid) as countposts
from posts as p
join users as u
on p.userid = u.userid
where p.likes >= 100
group by 1
order by 2 desc, 1 asc
limit 3
`"
Bradley E. - "Here's a simpler solution:
select
u.username
, count(p.postid) as countposts
from posts as p
join users as u
on p.userid = u.userid
where p.likes >= 100
group by 1
order by 2 desc, 1 asc
limit 3
`"See full answer
"Limit and rank() only works if there are no 2 employees with same salary ( which is okay for this use case)
For the query to pass all the test results, we need to use dense_rank
with ranked_employees as
(
select id, firstname, lastname, salary,
denserank() over(order by salary desc) as salaryrank
from employees
)
select id, firstname, lastname, salary from ranked_employees
where salary_rank <= 3
`"
Vysali K. - "Limit and rank() only works if there are no 2 employees with same salary ( which is okay for this use case)
For the query to pass all the test results, we need to use dense_rank
with ranked_employees as
(
select id, firstname, lastname, salary,
denserank() over(order by salary desc) as salaryrank
from employees
)
select id, firstname, lastname, salary from ranked_employees
where salary_rank <= 3
`"See full answer
"SELECT d.name as departmentname,e.id as employeeid,e.firstname,e.lastname,MAX(e.salary) as salary
FROM employees e LEFT JOIN departments d
ON e.department_id=d.id
GROUP BY department_name
ORDER BY department_name;"
Anisha S. - "SELECT d.name as departmentname,e.id as employeeid,e.firstname,e.lastname,MAX(e.salary) as salary
FROM employees e LEFT JOIN departments d
ON e.department_id=d.id
GROUP BY department_name
ORDER BY department_name;"See full answer
"we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"
Kishor J. - "we can use two pointer + set like maintain i,j and also insert jth character to set like while set size is equal to our window j-i+1 then maximize our answer and increase jth pointer till last index"See full answer
"// C++ program to print the count of
// subsets with sum equal to the given value X
#include
using namespace std;
// Recursive function to return the count
// of subsets with sum equal to the given value
int subsetSum(int arr[], int n, int i,
int sum, int count)
{
// The recursion is stopped at N-th level
// where all the subsets of the given array
// have been checked
if (i == n) {
// Incrementing the count if sum is
// equal to 0 and returning the count
if (sum == 0)"
Ajay U. - "// C++ program to print the count of
// subsets with sum equal to the given value X
#include
using namespace std;
// Recursive function to return the count
// of subsets with sum equal to the given value
int subsetSum(int arr[], int n, int i,
int sum, int count)
{
// The recursion is stopped at N-th level
// where all the subsets of the given array
// have been checked
if (i == n) {
// Incrementing the count if sum is
// equal to 0 and returning the count
if (sum == 0)"See full answer
"SELECT
u.user_id,
u.user_name,
u.email,
ROUND(AVG(CASE WHEN b.status = 'Unmatched' THEN 1.0 ELSE 0 END), 2) AS avgunmatchedbookings
FROM
users u
LEFT JOIN
bookings b ON u.userid = b.userid
GROUP BY
u.user_id,
u.user_name,
u.email;
`"
Akshay D. - "SELECT
u.user_id,
u.user_name,
u.email,
ROUND(AVG(CASE WHEN b.status = 'Unmatched' THEN 1.0 ELSE 0 END), 2) AS avgunmatchedbookings
FROM
users u
LEFT JOIN
bookings b ON u.userid = b.userid
GROUP BY
u.user_id,
u.user_name,
u.email;
`"See full answer
"#inplace reversal without inbuilt functions
def reverseString(s):
chars = list(s)
l, r = 0, len(s)-1
while l < r:
chars[l],chars[r] = chars[r],chars[l]
l += 1
r -= 1
reversed = "".join(chars)
return reversed
"
Anonymous Possum - "#inplace reversal without inbuilt functions
def reverseString(s):
chars = list(s)
l, r = 0, len(s)-1
while l < r:
chars[l],chars[r] = chars[r],chars[l]
l += 1
r -= 1
reversed = "".join(chars)
return reversed
"See full answer
"Required output in the solution not the one requested from the question. only customerid, firstname, last_name and years were required. Please this needs to be very clear.
Otherwise my answer is
with totalorderyear as (
SELECT
o.customer_id,
c.first_name,
c.last_name,
EXTRACT(YEAR FROM o.orderdate) AS orderyear,
COUNT(o.orderid) AS totalorders
FROM orders o
LEFT JOIN customers c
ON c.customerid = o.customerid
GROUP BY o.customerid, c.firstname, c.last"
Gloriose H. - "Required output in the solution not the one requested from the question. only customerid, firstname, last_name and years were required. Please this needs to be very clear.
Otherwise my answer is
with totalorderyear as (
SELECT
o.customer_id,
c.first_name,
c.last_name,
EXTRACT(YEAR FROM o.orderdate) AS orderyear,
COUNT(o.orderid) AS totalorders
FROM orders o
LEFT JOIN customers c
ON c.customerid = o.customerid
GROUP BY o.customerid, c.firstname, c.last"See full answer