Data Structures & Algorithms Interview Questions

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

    "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x. int low = 0; int high = n-1; while(low <= high){ int mid = (low + high) / 2; if(x == arr[mid]) return mid; //if x is already present then it will be the closest else if(x < arr[mid]) high"

    Shashwat K. - "Binary Search on the array and after than compare the numbers at low and the high pointers whichever is closest is the answer. Because after the binary search low will be pointing to a number which is immediate greater than x and high will be pointing to a number which is immediate lesser than x. int low = 0; int high = n-1; while(low <= high){ int mid = (low + high) / 2; if(x == arr[mid]) return mid; //if x is already present then it will be the closest else if(x < arr[mid]) high"See full answer

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

    "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"

    Mahaboob P. - "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"See full answer

    Data Engineer
    Data Structures & Algorithms
    +2 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Merge k sorted linked lists.'
    +6

    "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"

    Guilherme F. - "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"See full answer

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

    "function areSentencesSimilar(sentence1, sentence2, similarPairs) { if (sentence1.length !== sentence2.length) return false; for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1))); if (!edge) { "

    Tiago R. - "function areSentencesSimilar(sentence1, sentence2, similarPairs) { if (sentence1.length !== sentence2.length) return false; for (let i=0; i (w1 === word1 && !visited.has(w2)) || (w2 === word1 && !visited.has(w1))); if (!edge) { "See full answer

    Software Engineer
    Data Structures & Algorithms
    +2 more
  • +1

    "I think sliding window will work here and it is the most optimized approach to solve this question."

    Gaurav K. - "I think sliding window will work here and it is the most optimized approach to solve this question."See full answer

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

  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +4 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
    Data Structures & Algorithms
    +2 more
  • 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
    Data Structures & Algorithms
    +2 more
  • Accenture logoAsked at Accenture 

    "NA"

    Gaddipati V. - "NA"See full answer

    Software Engineer
    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
    Data Structures & Algorithms
    +2 more
  • "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
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Sort a doubly linked list using merge sort.'
    +4

    "function merge(L1, L2) { let L3 = { data: null, next: null }; let prev = L3; while (L1 != null || L2 != null) { if (L1 == null) { prev.next = L2; L2 = L2.next; } else if (L2 == null) { prev.next = L1; L1 = L1.next; } else if (L1.data < L2.data) { prev.next = L1; L1 = L1.next; } else { prev.next = L2; L2 = L2.next; } prev = prev.next; } return L3.next; }"

    Ugo C. - "function merge(L1, L2) { let L3 = { data: null, next: null }; let prev = L3; while (L1 != null || L2 != null) { if (L1 == null) { prev.next = L2; L2 = L2.next; } else if (L2 == null) { prev.next = L1; L1 = L1.next; } else if (L1.data < L2.data) { prev.next = L1; L1 = L1.next; } else { prev.next = L2; L2 = L2.next; } prev = prev.next; } return L3.next; }"See full answer

    Data Structures & Algorithms
    Coding
    +1 more
  • Adobe logoAsked at Adobe 
    Video answer for 'Solve John Conway's "Game of Life".'
    Software Engineer
    Data Structures & Algorithms
    +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
    Data Structures & Algorithms
    +2 more
  • Data Structures & Algorithms
    Coding
  • Spotify logoAsked at Spotify 

    Balanced Tree

    IDE
    Medium
    +5

    "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
    Data Structures & Algorithms
    +1 more
  • +9

    "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

    Data Structures & Algorithms
    Coding
  • Microsoft logoAsked at Microsoft 
    Video answer for 'Find the number of rotations in a circularly sorted array.'
    +8

    "function countRotations(arr, low, high) { if (high low && arr[mid] arr[mid]) { return countRotations(arr, low, mid - 1); } return countRotations(arr, mid + 1, high); } "

    Ugo C. - "function countRotations(arr, low, high) { if (high low && arr[mid] arr[mid]) { return countRotations(arr, low, mid - 1); } return countRotations(arr, mid + 1, high); } "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • 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
    Data Structures & Algorithms
    +1 more
  • +7

    "My solution is simple; it does an in-order DFS traversal to create an array of in-order elements then it searches through the array to find the node we want the successor of. finally we return the node that is 1 after the input node, in the case our input node is the last element of our DFS we know there is no successor, therefore it returns None/null. CODE INSTRUCTIONS: 1) The method fi"

    Rohan M. - "My solution is simple; it does an in-order DFS traversal to create an array of in-order elements then it searches through the array to find the node we want the successor of. finally we return the node that is 1 after the input node, in the case our input node is the last element of our DFS we know there is no successor, therefore it returns None/null. CODE INSTRUCTIONS: 1) The method fi"See full answer

    Data Structures & Algorithms
    Coding
Showing 101-120 of 241