Coding Interview Questions

Review this list of 374 coding interview questions and answers verified by hiring managers and candidates.
  • Nvidia logoAsked at Nvidia 

    "`#include using namespace std; void printNumbersTillN(int n){ if(n_==0){ return; } printNumbersTillN(n-1); // go to the end -> reach 1 cout>_n; printNumbersTillN(n); }`"

    Jet 1. - "`#include using namespace std; void printNumbersTillN(int n){ if(n_==0){ return; } printNumbersTillN(n-1); // go to the end -> reach 1 cout>_n; printNumbersTillN(n); }`"See full answer

    Software Engineer
    Coding
  • +5

    "not sure what's wrong here> select a.marketing_channel, avg(purchasevalue) as avgpurchase_value, sum(case when a.purchasevalue > 0 then 1 else 0 end) * 1.0 /count(a.sessionid) as conversion_rate from attribution a left join usersessions u on a.sessionid = u.session_id group by a.marketing_channel order by conversion_rate desc `"

    Shriganesh K. - "not sure what's wrong here> select a.marketing_channel, avg(purchasevalue) as avgpurchase_value, sum(case when a.purchasevalue > 0 then 1 else 0 end) * 1.0 /count(a.sessionid) as conversion_rate from attribution a left join usersessions u on a.sessionid = u.session_id group by a.marketing_channel order by conversion_rate desc `"See full answer

    Coding
    SQL
  • +4

    "SELECT order_amount FROM ( SELECT *, rank() OVER(ORDER BY order_amount desc) as ranking FROM departments d LEFT JOIN orders o ON d.departmentid = o.departmentid LEFT JOIN customers c ON o.customerid = c.customerid WHERE department_name = 'Fashion' ) where ranking = 2"

    Jacky T. - "SELECT order_amount FROM ( SELECT *, rank() OVER(ORDER BY order_amount desc) as ranking FROM departments d LEFT JOIN orders o ON d.departmentid = o.departmentid LEFT JOIN customers c ON o.customerid = c.customerid WHERE department_name = 'Fashion' ) where ranking = 2"See full answer

    Coding
    SQL
  • +11

    "function spiralCopy(inputMatrix) { if (inputMatrix.length === 1) return inputMatrix[0]; let lowerY = 0; let upperY = inputMatrix.length-1; let lowerX = 0; let upperX = inputMatrix[0].length-1; const output = []; while (true) { if (lowerX > upperX) break; for (let x = lowerX; x upperY) break; for (let y = lowerY; y <= upperY; y++) { output.push(inputMatrix[y][u"

    Tiago R. - "function spiralCopy(inputMatrix) { if (inputMatrix.length === 1) return inputMatrix[0]; let lowerY = 0; let upperY = inputMatrix.length-1; let lowerX = 0; let upperX = inputMatrix[0].length-1; const output = []; while (true) { if (lowerX > upperX) break; for (let x = lowerX; x upperY) break; for (let y = lowerY; y <= upperY; y++) { output.push(inputMatrix[y][u"See full answer

    Coding
    Data Structures & Algorithms
  • Amazon logoAsked at Amazon 
    +1

    "To determine if a graph is not a tree, you can check for the following conditions: Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree. Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree. Disconnected components: A tree is a connected graph, m"

    Vaibhav C. - "To determine if a graph is not a tree, you can check for the following conditions: Presence of cycles: A graph is not a tree if it contains cycles. In a tree, there should be exactly one unique path between any two vertices. If you can find a cycle in the graph, it cannot be a tree. Insufficient number of edges: A tree with N vertices will have exactly N-1 edges. If the graph has fewer or more than N-1 edges, then it is not a tree. Disconnected components: A tree is a connected graph, m"See full answer

    Software Engineer
    Coding
    +2 more
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • +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
  • +3

    "-- filter for december and november data -- the total order amount per depatment per month -- department, month, order_amount with monthly_orders AS ( SELECT department_id, strftime('%m', order_date) AS month, SUM(orderamount) AS orderamount FROM orders WHERE orderdate >= '2022-11-01' AND orderdate < '2023-01-01' group by department_id, month ), -- -- add difference from this month to last ( use lag ) monthly_comp"

    Aneesha K. - "-- filter for december and november data -- the total order amount per depatment per month -- department, month, order_amount with monthly_orders AS ( SELECT department_id, strftime('%m', order_date) AS month, SUM(orderamount) AS orderamount FROM orders WHERE orderdate >= '2022-11-01' AND orderdate < '2023-01-01' group by department_id, month ), -- -- add difference from this month to last ( use lag ) monthly_comp"See full answer

    Coding
    SQL
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Sort a doubly linked list using merge sort.'
    +4

    " from typing import Optional class Node: def init(self, val: int, prev: Optional['Node'] = None, next: Optional['Node'] = None): self.val = val self.prev = prev self.next = next def split(head): if not head or not head.next: return head slow = head fast = head.next while fast and fast.next: slow = slow.next fast = fast.next.next mid = slow.next slow.next = None if mid: mid.prev = None "

    Akash C. - " from typing import Optional class Node: def init(self, val: int, prev: Optional['Node'] = None, next: Optional['Node'] = None): self.val = val self.prev = prev self.next = next def split(head): if not head or not head.next: return head slow = head fast = head.next while fast and fast.next: slow = slow.next fast = fast.next.next mid = slow.next slow.next = None if mid: mid.prev = None "See full answer

    Coding
    Data Structures & Algorithms
    +1 more
  • +5

    "function knapsack(weights, values, cap) { const indicesByValue = Object.keys(weights).map(weight => parseInt(weight)); indicesByValue.sort((a, b) => values[b]-values[a]); const steps = new Map(); function knapsackStep(cap, sack) { if (steps.has(sack)) { return steps.get(sack); } let maxOutput = 0; for (let index of indicesByValue) { if (!sack.has(index) && weights[index] <= cap) { maxOutput ="

    Tiago R. - "function knapsack(weights, values, cap) { const indicesByValue = Object.keys(weights).map(weight => parseInt(weight)); indicesByValue.sort((a, b) => values[b]-values[a]); const steps = new Map(); function knapsackStep(cap, sack) { if (steps.has(sack)) { return steps.get(sack); } let maxOutput = 0; for (let index of indicesByValue) { if (!sack.has(index) && weights[index] <= cap) { maxOutput ="See full answer

    Software Engineer
    Coding
    +2 more
  • "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
  • Spotify logoAsked at Spotify 

    Balanced Tree

    IDE
    Medium
    +6

    "function visitChildren(node) { let leftSubtreeHeight = 0; let rightSubtreeHeight = 0; let isChildrenBalanced = true; if (node.left) { const { isBalanced, height } = visitChildren(node.left); isChildrenBalanced = isChildrenBalanced && isBalanced; leftSubtreeHeight += height + 1; } if (isChildrenBalanced && node.right) { const { isBalanced, height } = visitChildren(node.right); isChildrenBalanced = isChildrenBalanced && isBalan"

    Tiago R. - "function visitChildren(node) { let leftSubtreeHeight = 0; let rightSubtreeHeight = 0; let isChildrenBalanced = true; if (node.left) { const { isBalanced, height } = visitChildren(node.left); isChildrenBalanced = isChildrenBalanced && isBalanced; leftSubtreeHeight += height + 1; } if (isChildrenBalanced && node.right) { const { isBalanced, height } = visitChildren(node.right); isChildrenBalanced = isChildrenBalanced && isBalan"See full answer

    Software Engineer
    Coding
    +1 more
  • Apple logoAsked at Apple 

    "we can create 2 sets for rows and columns and store the rows and column having 0 and then just check in a loop if the count of that row is greater than 0 llly for column then put the row and column to zero"

    Bhavya V. - "we can create 2 sets for rows and columns and store the rows and column having 0 and then just check in a loop if the count of that row is greater than 0 llly for column then put the row and column to zero"See full answer

    Data Engineer
    Coding
    +2 more
  • Microsoft logoAsked at Microsoft 
    Video answer for 'Find the number of rotations in a circularly sorted array.'
    +8

    "function findRotations(nums) { if (nums.length 0 && nums[mid] > nums[mid-1]) { left = mid; } else { right = mid; } } return rig"

    Tiago R. - "function findRotations(nums) { if (nums.length 0 && nums[mid] > nums[mid-1]) { left = mid; } else { right = mid; } } return rig"See full answer

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

    "Coded the solution using this approach that is frequency table and counting. it is Leetcode 791"

    Anonymous Porcupine - "Coded the solution using this approach that is frequency table and counting. it is Leetcode 791"See full answer

    Mobile Engineer
    Coding
  • "class Solution: def missingNumber(self, nums: list[int]) -> int: Sorting approach n = len(nums) s = n*(n+1)//2 r = s - sum(nums) return self.r l = [3,0,1] print(missingNumber(l))"

    Rohit B. - "class Solution: def missingNumber(self, nums: list[int]) -> int: Sorting approach n = len(nums) s = n*(n+1)//2 r = s - sum(nums) return self.r l = [3,0,1] print(missingNumber(l))"See full answer

    Software Engineer
    Coding
    +1 more
  • Uber logoAsked at Uber 

    "Not my answer, but rather the details of this question. It should include the following functions: int insertNewCustomer(double revenue) -> returns a customer ID (assume auto-incremented & 0-based) int insertNewCustomer(double revenue, int referrerID) -> returns a customer ID (assume auto-incremented & 0-based) Set getLowestKCustomersByMinTotalRevenue(int k, double minTotalRevenue) -> returns customer IDs Note: The total revenue consists of the revenue that this customer bring"

    Anzhe M. - "Not my answer, but rather the details of this question. It should include the following functions: int insertNewCustomer(double revenue) -> returns a customer ID (assume auto-incremented & 0-based) int insertNewCustomer(double revenue, int referrerID) -> returns a customer ID (assume auto-incremented & 0-based) Set getLowestKCustomersByMinTotalRevenue(int k, double minTotalRevenue) -> returns customer IDs Note: The total revenue consists of the revenue that this customer bring"See full answer

    Data Engineer
    Coding
  • Adobe logoAsked at Adobe 
    +5

    "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

    Data Engineer
    Coding
    +4 more
  • +4

    "SELECT u.id as user_id, u.name, COUNT(t.product_id) AS orders FROM users u JOIN transactions t ON t.user_id = u.id JOIN products p ON p.id = t.product_id GROUP BY u.id, u.name ORDER BY orders DESC LIMIT 1 `"

    Derrick M. - "SELECT u.id as user_id, u.name, COUNT(t.product_id) AS orders FROM users u JOIN transactions t ON t.user_id = u.id JOIN products p ON p.id = t.product_id GROUP BY u.id, u.name ORDER BY orders DESC LIMIT 1 `"See full answer

    Coding
    SQL
  • +1

    "SELECT AVG(julianday(dateend) - julianday(datestart)) AS avgcampaignduration FROM campaign; `"

    Salome L. - "SELECT AVG(julianday(dateend) - julianday(datestart)) AS avgcampaignduration FROM campaign; `"See full answer

    Coding
    SQL
Showing 181-200 of 374