Software Engineer Data Structures & Algorithms Interview Questions

Review this list of 158 data structures & algorithms software engineer 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

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

    "public static List Merge(List array1, List array2) { if (array1.Count == 0) return array2; if (array2.Count == 0) return array1; var result = new List(); // Identify starting array, array with largest starting element var array = array1[0] > array2[0] ? array2 : array1; var other = array1[0] > array2[0] ? array1 : array2; // loop until we hit end of an array int arrayIndex = 0; int otherIndex = 0; while(arrayIndex"

    Mark S. - "public static List Merge(List array1, List array2) { if (array1.Count == 0) return array2; if (array2.Count == 0) return array1; var result = new List(); // Identify starting array, array with largest starting element var array = array1[0] > array2[0] ? array2 : array1; var other = array1[0] > array2[0] ? array1 : array2; // loop until we hit end of an array int arrayIndex = 0; int otherIndex = 0; while(arrayIndex"See full answer

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

  • 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
  • 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
  • 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

    Software Engineer
    Data Structures & Algorithms
    +3 more
  • Salesforce logoAsked at Salesforce 

    "Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."

    Nils G. - "Bitshift the number to the right and keep track of the 1's you encounter. If you bitshift it completely and only encounter one 1, it is a power of two."See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • 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
  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +2 more
  • "Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time. Maintain two zero-initialised variables - sum and count. Keep popping off the heap while sum < k, and update count. In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."

    Anonymous Wolf - "Construct a min-heap either inplace, or by making a copy of the array and then applying heapify on that copy. This is done in O(n) time. Maintain two zero-initialised variables - sum and count. Keep popping off the heap while sum < k, and update count. In the worst case you will have to do n pops, and each pop is O(log n), so the algorithm would take O(n log n) in total. Space complexity depends on whether you're allowed to modify inplace or not, so either O(1) or O(n) respectively."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 
    Software Engineer
    Data Structures & Algorithms
    +2 more
  • Amazon logoAsked at Amazon 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Asked at Confluent 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Software Engineer
    Data Structures & Algorithms
    +1 more
  • LinkedIn logoAsked at LinkedIn 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 

    Permutations

    IDE
    Medium

    "function permute(nums) { if (nums.length <= 1) { return [nums]; } const prevPermutations = permute(nums.slice(0, nums.length-1)); const currentNum = nums[nums.length-1]; const permutations = new Set(); for (let prev of prevPermutations) { for (let i=0; i < prev.length; i++) { permutations.add([...prev.slice(0, i), currentNum, ...prev.slice(i)]); } permutations.add([...prev, currentNum]); } return [...permutations]"

    Tiago R. - "function permute(nums) { if (nums.length <= 1) { return [nums]; } const prevPermutations = permute(nums.slice(0, nums.length-1)); const currentNum = nums[nums.length-1]; const permutations = new Set(); for (let prev of prevPermutations) { for (let i=0; i < prev.length; i++) { permutations.add([...prev.slice(0, i), currentNum, ...prev.slice(i)]); } permutations.add([...prev, currentNum]); } return [...permutations]"See full answer

    Software Engineer
    Data Structures & Algorithms
    +3 more
Showing 81-100 of 158