Coding Interview Questions

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

    "#simple solution 1.firstly find the node in the bst (O(logn) time complexity it take) 2.now removing the node consists of 3 cases: 1.if the node is leaf (no children): (keep track of parent and do) parent.left or parent.right=NULL simply remove the node () 2.if(has one child) replace the node with its child 3.if has both childs we replace the node with either inorder predesor(max of left tree)or inorder succesor and remove the node wh"

    Sambangi C. - "#simple solution 1.firstly find the node in the bst (O(logn) time complexity it take) 2.now removing the node consists of 3 cases: 1.if the node is leaf (no children): (keep track of parent and do) parent.left or parent.right=NULL simply remove the node () 2.if(has one child) replace the node with its child 3.if has both childs we replace the node with either inorder predesor(max of left tree)or inorder succesor and remove the node wh"See full answer

    Software Engineer
    Coding
  • +26

    "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

    Data Scientist
    Coding
    +1 more
  • "WITH ActiveUsersYesterday AS ( SELECT DISTINCT user_id FROM user_activity WHERE activity_date = CAST(GETDATE() - 1 AS DATE) ), VideoCallUsersYesterday AS ( SELECT DISTINCT user_id FROM video_calls WHERE call_date = CAST(GETDATE() - 1 AS DATE) ) SELECT (CAST(COUNT(DISTINCT v.userid) AS FLOAT) / NULLIF(COUNT(DISTINCT a.userid), 0)) * 100 AS percentagevideocall_users FROM ActiveUsersYesterday a LEFT JOIN VideoCallUsersYesterday v ON a.userid = v.userid;"

    Bala G. - "WITH ActiveUsersYesterday AS ( SELECT DISTINCT user_id FROM user_activity WHERE activity_date = CAST(GETDATE() - 1 AS DATE) ), VideoCallUsersYesterday AS ( SELECT DISTINCT user_id FROM video_calls WHERE call_date = CAST(GETDATE() - 1 AS DATE) ) SELECT (CAST(COUNT(DISTINCT v.userid) AS FLOAT) / NULLIF(COUNT(DISTINCT a.userid), 0)) * 100 AS percentagevideocall_users FROM ActiveUsersYesterday a LEFT JOIN VideoCallUsersYesterday v ON a.userid = v.userid;"See full answer

    Data Scientist
    Coding
    +2 more
  • +13

    "-- 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

    Coding
    SQL
  • +2

    "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."

    דניאל ר. - "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."See full answer

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

  • +20

    "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

    Data Scientist
    Coding
    +1 more
  • "Use Dutch National Flag Algorithm to solve the problem"

    Sireesha R. - "Use Dutch National Flag Algorithm to solve the problem"See full answer

    Software Engineer
    Coding
    +1 more
  • +40

    "#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

    Coding
    Data Structures & Algorithms
  • +2

    "class Solution { public boolean isValid(String s) { // Time Complexity and Space complexity will be O(n) Stack stack=new Stack(); for(char c:s.toCharArray()){ if(c=='('){ stack.push(')'); } else if(c=='{'){ stack.push('}'); } else if(c=='['){ stack.push(']'); } else if(stack.pop()!=c){ return false; } } return stack.isEmpty(); } }"

    Kanishvaran P. - "class Solution { public boolean isValid(String s) { // Time Complexity and Space complexity will be O(n) Stack stack=new Stack(); for(char c:s.toCharArray()){ if(c=='('){ stack.push(')'); } else if(c=='{'){ stack.push('}'); } else if(c=='['){ stack.push(']'); } else if(stack.pop()!=c){ return false; } } return stack.isEmpty(); } }"See full answer

    Machine Learning Engineer
    Coding
    +2 more
  • Adobe logoAsked at Adobe 
    +15

    "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

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

    "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

    Data Scientist
    Coding
    +1 more
  • +31

    "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

    Data Scientist
    Coding
    +1 more
  • TikTok logoAsked at TikTok 

    "class Solution: def lengthOfLIS(self, nums: List[int]) -> int: temp = [nums[0]] for num in nums: if temp[-1]< num: temp.append(num) else: index = bisect_left(temp,num) temp[index] = num return len(temp) "

    Mahima M. - "class Solution: def lengthOfLIS(self, nums: List[int]) -> int: temp = [nums[0]] for num in nums: if temp[-1]< num: temp.append(num) else: index = bisect_left(temp,num) temp[index] = num return len(temp) "See full answer

    Machine Learning Engineer
    Coding
    +1 more
  • Adobe logoAsked at Adobe 

    "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."

    Gaston B. - "Use a representative of each, e.g. sort the string and add it to the value of a hashmap> where we put all the words that belong to the same anagram together."See full answer

    Software Engineer
    Coding
    +4 more
  • Amazon logoAsked at Amazon 

    "class Node: def init(self, value): self.value = value self.children = [] def inorder_traversal(root): if not root: return [] result = [] n = len(root.children) for i in range(n): result.extend(inorder_traversal(root.children[i])) if i == n // 2: result.append(root.value) if n == 0: result.append(root.value) return result Example usage: root = Node(1) child1 = Node(2) chil"

    Teddy Y. - "class Node: def init(self, value): self.value = value self.children = [] def inorder_traversal(root): if not root: return [] result = [] n = len(root.children) for i in range(n): result.extend(inorder_traversal(root.children[i])) if i == n // 2: result.append(root.value) if n == 0: result.append(root.value) return result Example usage: root = Node(1) child1 = Node(2) chil"See full answer

    Software Engineer
    Coding
    +1 more
  • Coinbase logoAsked at Coinbase 
    Frontend Engineer
    Coding
  • +9

    "Would be better to adjust resolution in the video player directly."

    Anonymous Prawn - "Would be better to adjust resolution in the video player directly."See full answer

    Software Engineer
    Coding
    +4 more
  • +2

    "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"

    Dadja Z. - "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"See full answer

    Software Engineer
    Coding
    +2 more
  • "Started by asking clarification questions regarding design constraints and desired features. Break down the into re-usable components - HeroImg and Carousel (with 3 images rendered) Code the components and managed the state for both in the parent component."

    Akshay J. - "Started by asking clarification questions regarding design constraints and desired features. Break down the into re-usable components - HeroImg and Carousel (with 3 images rendered) Code the components and managed the state for both in the parent component."See full answer

    Frontend Engineer
    Coding
Showing 21-40 of 344