Data Structures & Algorithms Interview Questions

Review this list of 226 data structures & algorithms interview questions and answers verified by hiring managers and candidates.
  • 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
  • Apple logoAsked at Apple 
    Software Engineer
    Data Structures & Algorithms
    +2 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
  • +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
  • 🧠 Want an expert answer to a question? Saving questions lets us know what content to make next.

  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Software Engineer
    Data Structures & Algorithms
    +1 more
  • Asked at Confluent 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Find the common ancestors in a tree.'
    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • Amazon logoAsked at Amazon 
    Software Engineer
    Data Structures & Algorithms
    +1 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
  • Adobe logoAsked at Adobe 
    +16

    " 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
  • LinkedIn logoAsked at LinkedIn 
    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Adobe logoAsked at Adobe 
    Machine Learning Engineer
    Data Structures & Algorithms
    +2 more
  • +3

    "def flatten_dictionary(dictionary): \# return a flattened dictionary - int/string/another dictionary values \# if the key is empty, exclude from the output \# concat using a "." btwn them \# add to res which is { "key.a.b.etc": "value" } \# iterate through the key value pairs \# while there is a key value pair in the value \# continue going through that, until the value is an int/string flatDic = {} flatDicHelper("", dictionary, flatDic) print(flatDic) return flatDic def flatDicHelper(initialKey"

    Anonymous Owl - "def flatten_dictionary(dictionary): \# return a flattened dictionary - int/string/another dictionary values \# if the key is empty, exclude from the output \# concat using a "." btwn them \# add to res which is { "key.a.b.etc": "value" } \# iterate through the key value pairs \# while there is a key value pair in the value \# continue going through that, until the value is an int/string flatDic = {} flatDicHelper("", dictionary, flatDic) print(flatDic) return flatDic def flatDicHelper(initialKey"See full answer

    Data Structures & Algorithms
    Coding
  • 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
  • Apple logoAsked at Apple 

    "Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"

    Farhan -. - "Sorting is a technique to arrange data in either increasing order or decreasing order, and, the function that implements this functionality is called sort function"See full answer

    Software Engineer
    Data Structures & Algorithms
    +1 more
  • Visa logoAsked at Visa 

    "// Helper function to calculate the Euclidean distance between two points function distance(p1, p2) { return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2)); } // A helper function to find the closest pair in a given set of points within the strip function closestPairInStrip(strip, d) { let minDist = d; // Start with the current minimum distance strip.sort((a, b) => a[1] - b[1]); // Sort the strip by y-coordinate for (let i = 0; i < strip.length; i++) { "

    Vishnu V. - "// Helper function to calculate the Euclidean distance between two points function distance(p1, p2) { return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2)); } // A helper function to find the closest pair in a given set of points within the strip function closestPairInStrip(strip, d) { let minDist = d; // Start with the current minimum distance strip.sort((a, b) => a[1] - b[1]); // Sort the strip by y-coordinate for (let i = 0; i < strip.length; i++) { "See full answer

    Machine Learning Engineer
    Data Structures & Algorithms
    +1 more
  • +4

    "function getDifferentNumber(arr) { // your code goes here const n = arr.length; //Define Max Integer const MAX_INT = Math.pow(2, 31) - 1; //Coppy arr to arr1 then sort arr1. const arr1 = arr; arr1.sort(function(a,b) {return a -b}); // Put arr1 in Set to optimize lo const uniqueSet = new Set(arr1); console.log(uniqueSet); // Check for the smallest nonnegative integer not in the array for (let i = 0; i < n; i++) { if (!uniqueSet.has(i)) { return i; } } if(n<MAX_INT) return n+1; else return -1; }"

    Anonymous Hare - "function getDifferentNumber(arr) { // your code goes here const n = arr.length; //Define Max Integer const MAX_INT = Math.pow(2, 31) - 1; //Coppy arr to arr1 then sort arr1. const arr1 = arr; arr1.sort(function(a,b) {return a -b}); // Put arr1 in Set to optimize lo const uniqueSet = new Set(arr1); console.log(uniqueSet); // Check for the smallest nonnegative integer not in the array for (let i = 0; i < n; i++) { if (!uniqueSet.has(i)) { return i; } } if(n<MAX_INT) return n+1; else return -1; }"See full answer

    Data Structures & Algorithms
    Coding
  • Software Engineer
    Data Structures & Algorithms
    +1 more
Showing 121-140 of 226