Data Structures & Algorithms Interview Questions

Review this list of 241 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • +42

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

    Data Structures & Algorithms
    Coding
  • Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • +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
    Data Structures & Algorithms
    +2 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +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
    Data Structures & Algorithms
    +2 more
  • +10

    "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
    Data Structures & Algorithms
    +4 more
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • 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
    Data Structures & Algorithms
    +4 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
    Data Structures & Algorithms
    +1 more
  • "/* You are with your friends in a castle, where there are multiple rooms named after flowers. Some of the rooms contain treasures - we call them the treasure rooms. Each room contains a single instruction that tells you which room to go to next. * instructions1 and treasurerooms_1 * lily* --------- daisy sunflower | | | v v v jasmin --> tulip* violet* ----> rose* --> ^ | ^ ^ | "

    Azeezat R. - "/* You are with your friends in a castle, where there are multiple rooms named after flowers. Some of the rooms contain treasures - we call them the treasure rooms. Each room contains a single instruction that tells you which room to go to next. * instructions1 and treasurerooms_1 * lily* --------- daisy sunflower | | | v v v jasmin --> tulip* violet* ----> rose* --> ^ | ^ ^ | "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Apple logoAsked at Apple 
    +14

    "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

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • Atlassian logoAsked at Atlassian 
    Video answer for 'How would you store a list of numbers as a single number?'
    +7

    "First thing the interviewee did wrong is not asking clarifying questions. This is the most vague problem I have every heard, and the interviewee just made assumptions and started programming."

    Nicholas S. - "First thing the interviewee did wrong is not asking clarifying questions. This is the most vague problem I have every heard, and the interviewee just made assumptions and started programming."See full answer

    Engineering Manager
    Data Structures & Algorithms
    +2 more
  • Google logoAsked at Google 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Amazon logoAsked at Amazon 
    +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
    Data Structures & Algorithms
    +2 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
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Explain how to find a target sum in an array.'
    +4

    "a top-down recursive solution with memoization in python: from typing import List from functools import cache def changeSigns(nums: List[int], S: int) -> int: @cache def dp(i, curr_sum): if i == len(nums): if curr_sum == S: return 1 return 0 return dp(i+1, currsum + nums[i]) + dp(i+1, currsum - nums[i]) answer = dp(0, 0) if answer == 0: return -1 return answer `"

    Jessica R. - "a top-down recursive solution with memoization in python: from typing import List from functools import cache def changeSigns(nums: List[int], S: int) -> int: @cache def dp(i, curr_sum): if i == len(nums): if curr_sum == S: return 1 return 0 return dp(i+1, currsum + nums[i]) + dp(i+1, currsum - nums[i]) answer = dp(0, 0) if answer == 0: return -1 return answer `"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    +21

    "Idea for solution: Reverse the complete char array Reverse the words separated by space. i.e. Find the space characters and the reverse the subarray between two space characters. vector reverseSubarray(vector& arr, int s, int e) { while (s reverseWords(vector& arr ) { int n = arr.size(); reverse(arr, 0, n - 1"

    Rahul M. - "Idea for solution: Reverse the complete char array Reverse the words separated by space. i.e. Find the space characters and the reverse the subarray between two space characters. vector reverseSubarray(vector& arr, int s, int e) { while (s reverseWords(vector& arr ) { int n = arr.size(); reverse(arr, 0, n - 1"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Merge Intervals'
    +35

    "const mergeIntervals = (intervals) => { const compare = (a, b) => { if(a[0] b[0]) return 1 else if(a[0] === b[0]) { return a[1] - b[1] } } let current = [] const result = [] const sorted = intervals.sort(compare) for(let i = 0; i = b[0]) current[1] = b[1] els"

    Kofi N. - "const mergeIntervals = (intervals) => { const compare = (a, b) => { if(a[0] b[0]) return 1 else if(a[0] === b[0]) { return a[1] - b[1] } } let current = [] const result = [] const sorted = intervals.sort(compare) for(let i = 0; i = b[0]) current[1] = b[1] els"See full answer

    Software Engineer
    Data Structures & Algorithms
    +6 more
  • Goldman Sachs logoAsked at Goldman Sachs 

    "I solved it using recursion and then memoization. Used Dynamic programming approach"

    Ravi teja N. - "I solved it using recursion and then memoization. Used Dynamic programming approach"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Microsoft logoAsked at Microsoft 

    "Let me try to explain it with simple life analogy You're cooking dinner in the kitchen. Multithreading is when you've got a bunch of friends helping out. Each friend does a different job—like one chops veggies while another stirs a sauce. Everyone focuses on their task, and together, you all make the meal faster. In a computer, it's like different jobs happening all at once, making stuff happen quicker, just like having lots of friends helping makes dinner ready faster."

    Praveen D. - "Let me try to explain it with simple life analogy You're cooking dinner in the kitchen. Multithreading is when you've got a bunch of friends helping out. Each friend does a different job—like one chops veggies while another stirs a sauce. Everyone focuses on their task, and together, you all make the meal faster. In a computer, it's like different jobs happening all at once, making stuff happen quicker, just like having lots of friends helping makes dinner ready faster."See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Google logoAsked at Google 
    +1

    "You can ask some clarifying questions like 1) Ask if the list is already sorted or not 2) is zero included in the list ? 3) Natural numbers are usually positive numbers ( clarify they are non negatives) Solution : 1) If sorted use two pointers and sort them in O(N) 2) if not sorted , -ve / only +ve numbers in the list doesn't matter - the easiest solution is Use a priority queue and push the number and its square in each iteration Finally return the list returned by the priority Queue. N"

    Bless M. - "You can ask some clarifying questions like 1) Ask if the list is already sorted or not 2) is zero included in the list ? 3) Natural numbers are usually positive numbers ( clarify they are non negatives) Solution : 1) If sorted use two pointers and sort them in O(N) 2) if not sorted , -ve / only +ve numbers in the list doesn't matter - the easiest solution is Use a priority queue and push the number and its square in each iteration Finally return the list returned by the priority Queue. N"See full answer

    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
Showing 21-40 of 241