Coding Interview Questions

Review this list of 415 coding interview questions and answers verified by hiring managers and candidates.
  • +9

    "WITH previous AS(SELECT viewer_id, watch_hours, LAG(watchhours) OVER(PARTITION BY viewerid ORDER BY year, month) AS previous_hours, year, month FROM watch_time GROUP BY viewer_id, year, month ), streaks AS(SELECT viewer_id, SUM(CASE WHEN previoushours IS NOT NULL AND previoushours = 3 `"

    Alvin P. - "WITH previous AS(SELECT viewer_id, watch_hours, LAG(watchhours) OVER(PARTITION BY viewerid ORDER BY year, month) AS previous_hours, year, month FROM watch_time GROUP BY viewer_id, year, month ), streaks AS(SELECT viewer_id, SUM(CASE WHEN previoushours IS NOT NULL AND previoushours = 3 `"See full answer

    Coding
    SQL
  • +6

    "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

    Data Analyst
    Coding
    +1 more
  • "You are given a string S and a number K as input, and your task is to print S to console output considering that, at most, you can print K characters per line. Example: S = "abracadabra sample" K = 11 Output: abracadabra sample Note that this problem requires the interviewee gather extra requirements from the interviewer (e.g. do we care about multiple white spaces? what if the length of a word is greater than K, ...)"

    B. T. - "You are given a string S and a number K as input, and your task is to print S to console output considering that, at most, you can print K characters per line. Example: S = "abracadabra sample" K = 11 Output: abracadabra sample Note that this problem requires the interviewee gather extra requirements from the interviewer (e.g. do we care about multiple white spaces? what if the length of a word is greater than K, ...)"See full answer

    Software Engineer
    Coding
    +1 more
  • +11

    "import Foundation func spiralCopy(inputMatrix: [[Int]]) -> [Int] { let arr = inputMatrix var top = 0, down = arr.count - 1 var left = 0, right = arr[0].count - 1 if top == down && left == right { return arr[top] } var ans: [Int] = [] while top <= down && left <= right { for i in left..<right { ans.append(arrtop) } for i in top..<down { ans.append(arri) } fo"

    Reno S. - "import Foundation func spiralCopy(inputMatrix: [[Int]]) -> [Int] { let arr = inputMatrix var top = 0, down = arr.count - 1 var left = 0, right = arr[0].count - 1 if top == down && left == right { return arr[top] } var ans: [Int] = [] while top <= down && left <= right { for i in left..<right { ans.append(arrtop) } for i in top..<down { ans.append(arri) } fo"See full answer

    Coding
    Data Structures & Algorithms
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • Cursor logoAsked at Cursor 
    Software Engineer
    Coding
  • Apple logoAsked at Apple 
    +2

    "This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array. We are not going to mo"

    Bhaskar B. - "This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array. We are not going to mo"See full answer

    Software Engineer
    Coding
    +2 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 

    "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

    Machine Learning Engineer
    Coding
    +1 more
  • +3

    "I would avoid converting order_date WITH monthly_totals AS ( SELECT department_id, SUM(CASE WHEN DATETRUNC('month', orderdate) = '2022-11-01' THEN orderamount ELSE 0 END) AS novtotal, SUM(CASE WHEN DATETRUNC('month', orderdate) = '2022-12-01' THEN orderamount ELSE 0 END) AS dectotal FROM orders WHERE order_date BETWEEN '2022-11-01' AND '2022-12-31' GROUP BY department_id ), mom_increases AS ( SELECT "

    Jaime A. - "I would avoid converting order_date WITH monthly_totals AS ( SELECT department_id, SUM(CASE WHEN DATETRUNC('month', orderdate) = '2022-11-01' THEN orderamount ELSE 0 END) AS novtotal, SUM(CASE WHEN DATETRUNC('month', orderdate) = '2022-12-01' THEN orderamount ELSE 0 END) AS dectotal FROM orders WHERE order_date BETWEEN '2022-11-01' AND '2022-12-31' GROUP BY department_id ), mom_increases AS ( SELECT "See full answer

    Coding
    SQL
  • +7

    "with t1 as (select employee_name, department_id, salary, avg(salary) over (partition by departmentid) as avgsalary, abs(salary - avg(salary) over (partition by department_id)) as diff from employees ) select employee_name, department_id, salary, avg_salary, denserank() over (partition by departmentid order by diff desc) as deviation_rank from t1 order by departmentid asc, deviationrank asc, employee_name `"

    Alexey T. - "with t1 as (select employee_name, department_id, salary, avg(salary) over (partition by departmentid) as avgsalary, abs(salary - avg(salary) over (partition by department_id)) as diff from employees ) select employee_name, department_id, salary, avg_salary, denserank() over (partition by departmentid order by diff desc) as deviation_rank from t1 order by departmentid asc, deviationrank asc, employee_name `"See full answer

    Coding
    SQL
  • "Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."

    Techzen I. - "Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."See full answer

    Machine Learning Engineer
    Coding
    +2 more
  • +9

    "Node* getLeftMostChild(Node* node){ while(node->left){ node = node->left; } return node; } Node* findInOrderSuccessor( Node *inputNode ) { int val = inputNode->key; if(inputNode->right){ return getLeftMostChild(inputNode->right); }else{ inputNode = inputNode->parent; while(inputNode && inputNode->key parent; } return inputNode; } } "

    Jack99 - "Node* getLeftMostChild(Node* node){ while(node->left){ node = node->left; } return node; } Node* findInOrderSuccessor( Node *inputNode ) { int val = inputNode->key; if(inputNode->right){ return getLeftMostChild(inputNode->right); }else{ inputNode = inputNode->parent; while(inputNode && inputNode->key parent; } return inputNode; } } "See full answer

    Coding
    Data Structures & Algorithms
  • "1) create the experimental and control groups. 2) Then calculate the proportion (mean) of the true conversion rates for both groups using the convert column which counts True as 1 and False as 0. This is their conversion rates 3) calculate the statistic of the two groups by subtracting the proportion and standardizing. 4) get the p-value and compare with 0.05. 5) conclude the difference is statistically significant if the p-value is less than 0.05 otherwise no statistical difference"

    Frank A. - "1) create the experimental and control groups. 2) Then calculate the proportion (mean) of the true conversion rates for both groups using the convert column which counts True as 1 and False as 0. This is their conversion rates 3) calculate the statistic of the two groups by subtracting the proportion and standardizing. 4) get the p-value and compare with 0.05. 5) conclude the difference is statistically significant if the p-value is less than 0.05 otherwise no statistical difference"See full answer

    Coding
    Machine Learning
  • Airbnb logoAsked at Airbnb 
    Video answer for 'Find the minimum window substring.'

    "What about exploiting the hash set and that is it? def smallestSubstring(s: str, t: str) -> str: if len(t) > len(s): return "" r = len(s) - 1 not_found = True while r > 0 and not_found: subs_set = set(s[0:r + 1]) for c in t: if not c in subs_set: not_found = False if not_found: r -= 1 else: r += 1 l = 0 not_found = True while l < r and not_"

    Gabriele G. - "What about exploiting the hash set and that is it? def smallestSubstring(s: str, t: str) -> str: if len(t) > len(s): return "" r = len(s) - 1 not_found = True while r > 0 and not_found: subs_set = set(s[0:r + 1]) for c in t: if not c in subs_set: not_found = False if not_found: r -= 1 else: r += 1 l = 0 not_found = True while l < r and not_"See full answer

    Software Engineer
    Coding
    +1 more
  • Apple logoAsked at Apple 

    "I was able to provide the optimal approach and coded it up"

    Anonymous Wasp - "I was able to provide the optimal approach and coded it up"See full answer

    Data Engineer
    Coding
    +2 more
  • +2

    "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

    Coding
    SQL
  • +4

    "select t.user_id, u.name, count(t.id) as orders from transactions t inner join users u on t.user_id=u.id group by 1,2 order by count(t.id) desc limit 1"

    מאיה ט. - "select t.user_id, u.name, count(t.id) as orders from transactions t inner join users u on t.user_id=u.id group by 1,2 order by count(t.id) desc limit 1"See full answer

    Coding
    SQL
  • Apple logoAsked at Apple 

    "A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"

    Alex M. - "A red-black tree is a self-balancing binary search tree. The motivation for this is that the benefits of O(logN) search, insertion, and deletion that a binary tree provides us will disappear if we let the tree get too "imbalanced" (e.g. there are too many nodes on one side of the tree or some branches have a depth that is way out of proportion to the average branch depth). This imbalance will occur if we don't adjust the tree after inserting or deleting nodes, hence our need for self-balancing c"See full answer

    Software Engineer
    Coding
    +1 more
  • +3

    "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

    Data Analyst
    Coding
    +1 more
Showing 221-240 of 415