Data Structures & Algorithms Interview Questions

Review this list of 226 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • 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
  • "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
  • 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
  • "Depend on the array size and number of 0's theere."

    Nasit S. - "Depend on the array size and number of 0's theere."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

    "arr1.append(arr2); arr1.sort();"

    Gennady O. - "arr1.append(arr2); arr1.sort();"See full answer

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

  • "public static char getRepeatingCharacterInGivenString(String str){ char result = '0'; HashSet set = new HashSet(); for(int i=0;i<str.length();i++){ char c = str.charAt(i); if(!set.contains(c)){ set.add(c); } else{ result= c; break; } } return result; }"

    Sravanthi M. - "public static char getRepeatingCharacterInGivenString(String str){ char result = '0'; HashSet set = new HashSet(); for(int i=0;i<str.length();i++){ char c = str.charAt(i); if(!set.contains(c)){ set.add(c); } else{ result= c; break; } } return result; }"See full answer

    QA Engineer
    Data Structures & Algorithms
    +1 more
  • Salesforce logoAsked at Salesforce 
    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
  • +8

    "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
  • Lyft logoAsked at Lyft 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • +6

    "Node findInOrderSuccessor(Node inputNode) { // your code goes here int key = inputNode.key; Node currentNode = this.root; Node succ = null; while(currentNode != null){ if(currentNode.key > key){ succ = currentNode; currentNode = currentNode.left; } else { currentNode = currentNode.right; } } return succ; } `"

    Sam J. - "Node findInOrderSuccessor(Node inputNode) { // your code goes here int key = inputNode.key; Node currentNode = this.root; Node succ = null; while(currentNode != null){ if(currentNode.key > key){ succ = currentNode; currentNode = currentNode.left; } else { currentNode = currentNode.right; } } return succ; } `"See full answer

    Data Structures & Algorithms
    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
    Data Structures & Algorithms
    +4 more
  • Sales Path

    IDE
    Medium
    +6

    "def getcheapestcost(rootNode): \# need to do DFS for each branch \# but this can be done recursively n = len(rootNode.children) if n == 0: return 0 else: min_cost = float('inf') for i in range(len(n)): tempcost = getcheapest_cost(rootNode.children[i]) if (tempcost < mincost): mincost = tempcost return min_cost + rootNode.cost \# A node class Node: \# Constructor to create a new node def init\(self, cost): self.cost = cost self.children = [] self.parent = None"

    Anonymous Owl - "def getcheapestcost(rootNode): \# need to do DFS for each branch \# but this can be done recursively n = len(rootNode.children) if n == 0: return 0 else: min_cost = float('inf') for i in range(len(n)): tempcost = getcheapest_cost(rootNode.children[i]) if (tempcost < mincost): mincost = tempcost return min_cost + rootNode.cost \# A node class Node: \# Constructor to create a new node def init\(self, cost): self.cost = cost self.children = [] self.parent = None"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 
    +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
    Data Structures & Algorithms
    +2 more
  • +7

    "I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you! def encrypt(word): en_word = "" for i in range(len(word)): if i == 0: en_word += chr(ord(word[0])+1) else: num = ord(word[i]) + ord(en_word[i-1]) while num > 122: num -= 26 en_word += chr(num) return en_word def decrypt(word): de_word = "" for i in range(len(word)): if i == 0: de_word += chr(ord(word[i]"

    Anonymous Armadillo - "I couldn't follow the solution offered here, but my solution seemed to pass 6/6 tests. Any feedback is welcome, thank you! def encrypt(word): en_word = "" for i in range(len(word)): if i == 0: en_word += chr(ord(word[0])+1) else: num = ord(word[i]) + ord(en_word[i-1]) while num > 122: num -= 26 en_word += chr(num) return en_word def decrypt(word): de_word = "" for i in range(len(word)): if i == 0: de_word += chr(ord(word[i]"See full answer

    Data Structures & Algorithms
    Coding
  • Adobe logoAsked at Adobe 
    +2

    "public class sample { public int [] merge(int [] a, int [] b) { if(a == null || a.length == 0 || b == null || b.length == 0) return null; int i = 0, j = 0, index = -1; int [] merged = new int[a.length + b.length]; while (i < a.length && j < b.length) { if(a[i] < b[i]) merged[++index] = a[i++]; else merged[++index] = b[j++]; } while (i < a.length) { merged[++index] = a[i++]; } "

    Nikhil R. - "public class sample { public int [] merge(int [] a, int [] b) { if(a == null || a.length == 0 || b == null || b.length == 0) return null; int i = 0, j = 0, index = -1; int [] merged = new int[a.length + b.length]; while (i < a.length && j < b.length) { if(a[i] < b[i]) merged[++index] = a[i++]; else merged[++index] = b[j++]; } while (i < a.length) { merged[++index] = a[i++]; } "See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 more
  • "We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria). To develop a function to set this up. Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"

    Prashanth A. - "We are asked to calculate Sum(over value) for time in (t - window_size, t) where key in (key criteria). To develop a function to set this up. Let w be the window size. I would have an observer of some kind note the key-value, and for the first w windows just add the value to a temporary variable in memory if the key meets the key criteria. Then it would delete the oldest value and add the new value if the new key meets the criteria. At each step after "w", we would take the sum / w and store"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Airbnb logoAsked at Airbnb 
    Video answer for 'Find the minimum window substring.'

    "sliding window"

    Ashley M. - "sliding window"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Backend Engineer
    Data Structures & Algorithms
    +1 more
Showing 101-120 of 226