Data Structures & Algorithms Interview Questions

Review this list of 241 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • 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
  • 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
  • "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
  • Adobe logoAsked at Adobe 
    +2

    "function main(){ const v1=[2,3, 4, 10] const v2= [3,4 ,5,20, 23] return merge(v1,v2); } function merge(left, right){ const result=[]; while(left.length>0&& right.length>0){ if(left[0]0){ result=result.concat(left) } if(right.length>0){ result=result.concat(right) } return result; }"

    Samuel M. - "function main(){ const v1=[2,3, 4, 10] const v2= [3,4 ,5,20, 23] return merge(v1,v2); } function merge(left, right){ const result=[]; while(left.length>0&& right.length>0){ if(left[0]0){ result=result.concat(left) } if(right.length>0){ result=result.concat(right) } return result; }"See full answer

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

  • 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
  • "Example: bucket A: 3 liters capacity bucket B: 5 liters capacity goal: 4 liters You are asked to print the logical sequence to get to the 4 liters of water in one bucket. Follow up: How would you solve the problem if you have more than 2 buckets of water?"

    B. T. - "Example: bucket A: 3 liters capacity bucket B: 5 liters capacity goal: 4 liters You are asked to print the logical sequence to get to the 4 liters of water in one bucket. Follow up: How would you solve the problem if you have more than 2 buckets of water?"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
  • Data Structures & Algorithms
    Coding
  • Adobe logoAsked at Adobe 
    +17

    " O(n) time, O(1) space from typing import List def maxsubarraysum(nums: List[int]) -> int: if len(nums) == 0: return 0 maxsum = currsum = nums[0] for i in range(1, len(nums)): currsum = max(currsum + nums[i], nums[i]) maxsum = max(currsum, max_sum) return max_sum debug your code below print(maxsubarraysum([-1, 2, -3, 4])) `"

    Rick E. - " O(n) time, O(1) space from typing import List def maxsubarraysum(nums: List[int]) -> int: if len(nums) == 0: return 0 maxsum = currsum = nums[0] for i in range(1, len(nums)): currsum = max(currsum + nums[i], nums[i]) maxsum = max(currsum, max_sum) return max_sum debug your code below print(maxsubarraysum([-1, 2, -3, 4])) `"See full answer

    Software Engineer
    Data Structures & Algorithms
    +4 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
  • Lyft logoAsked at Lyft 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Salesforce logoAsked at Salesforce 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Asked at Confluent 

    "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
  • Apple logoAsked at Apple 

    "class TrieNode { constructor() { this.children = {}; this.isEndOfWord = false; } } class Trie { constructor() { this.root = new TrieNode(); } insert(word) { let node = this.root; for (const char of word) { if (!node.children[char]) { node.children[char] = new TrieNode(); } node = node.children[char]; } node.isEndOfWord = true; } search(word) { l"

    Tiago R. - "class TrieNode { constructor() { this.children = {}; this.isEndOfWord = false; } } class Trie { constructor() { this.root = new TrieNode(); } insert(word) { let node = this.root; for (const char of word) { if (!node.children[char]) { node.children[char] = new TrieNode(); } node = node.children[char]; } node.isEndOfWord = true; } search(word) { l"See full answer

    Data Engineer
    Data Structures & Algorithms
    +3 more
  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +2 more
  • +6

    "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
  • Nvidia logoAsked at Nvidia 

    "def containSubString(mainString, SubString): s1 = "hello world" # main String s2 = "hello" s3 = "world" s4 = "Nothing" answer1 = containSubString(s1, s2) answer2 = containSubString(s1, s3) answer3 = containSubString(s1, s4) print(answer1 , answer2, answer) "

    Jalpa S. - "def containSubString(mainString, SubString): s1 = "hello world" # main String s2 = "hello" s3 = "world" s4 = "Nothing" answer1 = containSubString(s1, s2) answer2 = containSubString(s1, s3) answer3 = containSubString(s1, s4) print(answer1 , answer2, answer) "See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Backend Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    Machine Learning Engineer
    Data Structures & Algorithms
    +2 more
Showing 121-140 of 241