Data Structures & Algorithms Interview Questions

Review this list of 255 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • 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
    Data Structures & Algorithms
    +1 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
  • +11

    "import Foundation func spiralCopy(inputMatrix: [[Int]]) -> [Int] { let arr = inputMatrix var top = 0, down = arr.count - 1 var left = 0, right = arr[0].count - 1 if top == down && left == right { return arr[top] } var ans: [Int] = [] while top <= down && left <= right { for i in left..<right { ans.append(arrtop) } for i in top..<down { ans.append(arri) } fo"

    Reno S. - "import Foundation func spiralCopy(inputMatrix: [[Int]]) -> [Int] { let arr = inputMatrix var top = 0, down = arr.count - 1 var left = 0, right = arr[0].count - 1 if top == down && left == right { return arr[top] } var ans: [Int] = [] while top <= down && left <= right { for i in left..<right { ans.append(arrtop) } for i in top..<down { ans.append(arri) } fo"See full answer

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

  • "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
  • Adobe logoAsked at Adobe 
    +3

    "def mergeTwoListsRecursive(l1, l2): if not l1 or not l2: return l1 or l2 if l1.val < l2.val: l1.next = mergeTwoListsRecursive(l1.next, l2) return l1 else: l2.next = mergeTwoListsRecursive(l1, l2.next) return l2 "

    Ramachandra N. - "def mergeTwoListsRecursive(l1, l2): if not l1 or not l2: return l1 or l2 if l1.val < l2.val: l1.next = mergeTwoListsRecursive(l1.next, l2) return l1 else: l2.next = mergeTwoListsRecursive(l1, l2.next) return l2 "See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 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
  • 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
  • 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
  • Airbnb logoAsked at Airbnb 
    Video answer for 'Find the minimum window substring.'

    "What about exploiting the hash set and that is it? def smallestSubstring(s: str, t: str) -> str: if len(t) > len(s): return "" r = len(s) - 1 not_found = True while r > 0 and not_found: subs_set = set(s[0:r + 1]) for c in t: if not c in subs_set: not_found = False if not_found: r -= 1 else: r += 1 l = 0 not_found = True while l < r and not_"

    Gabriele G. - "What about exploiting the hash set and that is it? def smallestSubstring(s: str, t: str) -> str: if len(t) > len(s): return "" r = len(s) - 1 not_found = True while r > 0 and not_found: subs_set = set(s[0:r + 1]) for c in t: if not c in subs_set: not_found = False if not_found: r -= 1 else: r += 1 l = 0 not_found = True while l < r and not_"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
  • Apple logoAsked at Apple 

    "public class HashMap { public class Element { T key; V value; Element(T k, V v) { this.key = k; this.value = v; } } private static final int DEFAULT_CAPACITY = 16; private static final float LOAD_FACTOR = 0.75f; private LinkedList[] table = new LinkedList[DEFAULT_CAPACITY]; private int size = 0; private int threshold = (int) (DEFAULTCAPACITY * LOADFACTOR); public void put(T k"

    Md kamrul H. - "public class HashMap { public class Element { T key; V value; Element(T k, V v) { this.key = k; this.value = v; } } private static final int DEFAULT_CAPACITY = 16; private static final float LOAD_FACTOR = 0.75f; private LinkedList[] table = new LinkedList[DEFAULT_CAPACITY]; private int size = 0; private int threshold = (int) (DEFAULTCAPACITY * LOADFACTOR); public void put(T k"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
  • Frontend Engineer
    Data Structures & Algorithms
    +1 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 
    Video answer for 'Solve John Conway's "Game of Life".'
    Software Engineer
    Data Structures & Algorithms
    +2 more
  • +8

    "Good Question, but I would've marked this as medium not hard difficulty, since it's just a straightforward traversal."

    Ahmed A. - "Good Question, but I would've marked this as medium not hard difficulty, since it's just a straightforward traversal."See full answer

    Data Structures & Algorithms
    Coding
  • 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
  • "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
Showing 121-140 of 255