Meta (Facebook) Coding Interview Questions

Review this list of 28 Meta (Facebook) coding software engineer interview questions and answers verified by hiring managers and candidates.
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +28

    "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach. Iterative approach (JavaScript) function reverseLL(head){ if(head === null) return head; let prv = null; let next = null; let cur = head; while(cur){ next = cur.next; //backup cur.next = prv; prv = cur; cur = next; } head = prv; return head; } Recursion Approach (JS) function reverseLLByRecursion("

    Satyam S. - "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach. Iterative approach (JavaScript) function reverseLL(head){ if(head === null) return head; let prv = null; let next = null; let cur = head; while(cur){ next = cur.next; //backup cur.next = prv; prv = cur; cur = next; } head = prv; return head; } Recursion Approach (JS) function reverseLLByRecursion("See full answer

    Software Engineer
    Coding
    +4 more
  • +20

    "Since the problem asks for a O(logN) solution, I have to assume that the numbers are already sorted, meaning the same number are adjacent to each other, the value of the numbers shouldn't matter, and they expect us to use Binary Search. First, we should analyze the pattern of a regular number array without a single disrupter. Index: 0 1 2 3 4. 5 6. 7. 8. 9 Array:[1, 1, 2, 2, 4, 4, 5, 5, 6, 6] notice the odd indexes are always referencing the second of the reoccurring numbers and t"

    Bamboo Y. - "Since the problem asks for a O(logN) solution, I have to assume that the numbers are already sorted, meaning the same number are adjacent to each other, the value of the numbers shouldn't matter, and they expect us to use Binary Search. First, we should analyze the pattern of a regular number array without a single disrupter. Index: 0 1 2 3 4. 5 6. 7. 8. 9 Array:[1, 1, 2, 2, 4, 4, 5, 5, 6, 6] notice the odd indexes are always referencing the second of the reoccurring numbers and t"See full answer

    Software Engineer
    Coding
  • +8

    "Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"

    Srikant V. - "Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"See full answer

    Software Engineer
    Coding
    +1 more
  • +2

    "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."

    דניאל ר. - "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."See full answer

    Software Engineer
    Coding
    +2 more
  • +2

    "class Solution { public boolean isValid(String s) { // Time Complexity and Space complexity will be O(n) Stack stack=new Stack(); for(char c:s.toCharArray()){ if(c=='('){ stack.push(')'); } else if(c=='{'){ stack.push('}'); } else if(c=='['){ stack.push(']'); } else if(stack.pop()!=c){ return false; } } return stack.isEmpty(); } }"

    Kanishvaran P. - "class Solution { public boolean isValid(String s) { // Time Complexity and Space complexity will be O(n) Stack stack=new Stack(); for(char c:s.toCharArray()){ if(c=='('){ stack.push(')'); } else if(c=='{'){ stack.push('}'); } else if(c=='['){ stack.push(']'); } else if(stack.pop()!=c){ return false; } } return stack.isEmpty(); } }"See full answer

    Software Engineer
    Coding
    +2 more
  • 🧠 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
    Coding
    +1 more
  • +2

    "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"

    Dadja Z. - "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"See full answer

    Software Engineer
    Coding
    +2 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Explain how to find a target sum in an array.'
    +2

    "A recursive backtracking solution in python. def changeSigns(nums: List[int], S: int) -> int: res = [] n = len(nums) def backtrack(index, curr, arr): if curr == S and len(arr) == n: res.append(arr[:]) return if index >= len(nums): return for i in range(index, n): add +ve number arr.append(nums[i]) backtrack(i+1, curr + nums[i], arr) arr.pop() "

    Yugaank K. - "A recursive backtracking solution in python. def changeSigns(nums: List[int], S: int) -> int: res = [] n = len(nums) def backtrack(index, curr, arr): if curr == S and len(arr) == n: res.append(arr[:]) return if index >= len(nums): return for i in range(index, n): add +ve number arr.append(nums[i]) backtrack(i+1, curr + nums[i], arr) arr.pop() "See full answer

    Software Engineer
    Coding
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Merge Intervals'
    +33

    "const mergeIntervals = (intervals) => { const compare = (a, b) => { if(a[0] b[0]) return 1 else if(a[0] === b[0]) { return a[1] - b[1] } } let current = [] const result = [] const sorted = intervals.sort(compare) for(let i = 0; i = b[0]) current[1] = b[1] els"

    Kofi N. - "const mergeIntervals = (intervals) => { const compare = (a, b) => { if(a[0] b[0]) return 1 else if(a[0] === b[0]) { return a[1] - b[1] } } let current = [] const result = [] const sorted = intervals.sort(compare) for(let i = 0; i = b[0]) current[1] = b[1] els"See full answer

    Software Engineer
    Coding
    +6 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +1

    "public class CircularBuffer { private T[] buffer; private int head; private int tail; private int size; private final int capacity; public CircularBuffer(int capacity) { this.capacity = capacity; this.buffer = (T[]) new Object[capacity]; this.head = 0; this.tail = 0; this.size = 0; } public void enqueue(T item) { if (isFull()) { throw new IllegalStateException("Buffer is full"); } buf"

    Vidhyadhar V. - "public class CircularBuffer { private T[] buffer; private int head; private int tail; private int size; private final int capacity; public CircularBuffer(int capacity) { this.capacity = capacity; this.buffer = (T[]) new Object[capacity]; this.head = 0; this.tail = 0; this.size = 0; } public void enqueue(T item) { if (isFull()) { throw new IllegalStateException("Buffer is full"); } buf"See full answer

    Software Engineer
    Coding
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +15

    "function isValid(s) { const stack = []; for (let i=0; i < s.length; i++) { const char = s.charAt(i); if (['(', '{', '['].includes(char)) { stack.push(char); } else { const top = stack.pop(); if ((char === ')' && top !== '(') || (char === '}' && top !== '{') || (char === ']' && top !== '[')) { return false; } } } return stack.length === 0"

    Tiago R. - "function isValid(s) { const stack = []; for (let i=0; i < s.length; i++) { const char = s.charAt(i); if (['(', '{', '['].includes(char)) { stack.push(char); } else { const top = stack.pop(); if ((char === ')' && top !== '(') || (char === '}' && top !== '{') || (char === ']' && top !== '[')) { return false; } } } return stack.length === 0"See full answer

    Software Engineer
    Coding
    +4 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Move all zeros to the end of an array.'
    +39

    "Initialize left pointer: Set a left pointer left to 0. Iterate through the array: Iterate through the array from left to right. If the current element is not 0, swap it with the element at the left pointer and increment left. Time complexity: O(n). The loop iterates through the entire array once, making it linear time. Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures. "

    Avon T. - "Initialize left pointer: Set a left pointer left to 0. Iterate through the array: Iterate through the array from left to right. If the current element is not 0, swap it with the element at the left pointer and increment left. Time complexity: O(n). The loop iterates through the entire array once, making it linear time. Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures. "See full answer

    Software Engineer
    Coding
    +4 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +1

    "my answer: void* memcpy(void* dest, const void* src, size_t n) { unsigned char* uDest = static_cast(dest); const unsigned char* ucSrc = static_cast(src); for(size_t i= 0; i(dest); const unsigned c"

    Srihitha J. - "my answer: void* memcpy(void* dest, const void* src, size_t n) { unsigned char* uDest = static_cast(dest); const unsigned char* ucSrc = static_cast(src); for(size_t i= 0; i(dest); const unsigned c"See full answer

    Software Engineer
    Coding
    +1 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +1

    "const ops = { '+': (a, b) => a+b, '-': (a, b) => a-b, '/': (a, b) => a/b, '': (a, b) => ab, }; function calc(expr) { // Search for + or - for (let i=expr.length-1; i >= 0; i--) { const char = expr.charAt(i); if (['+', '-'].includes(char)) { return opschar), calc(expr.slice(i+1))); } } // Search for / or * for (let i=expr.length-1; i >= 0; i--) { const char = expr.charAt(i); if"

    Tiago R. - "const ops = { '+': (a, b) => a+b, '-': (a, b) => a-b, '/': (a, b) => a/b, '': (a, b) => ab, }; function calc(expr) { // Search for + or - for (let i=expr.length-1; i >= 0; i--) { const char = expr.charAt(i); if (['+', '-'].includes(char)) { return opschar), calc(expr.slice(i+1))); } } // Search for / or * for (let i=expr.length-1; i >= 0; i--) { const char = expr.charAt(i); if"See full answer

    Software Engineer
    Coding
    +3 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +37

    "Arrays.sort(inputarray) sliding window with a size of 2. Check for the sum in the sliding window. subtract the start when window moves"

    Sridhar R. - "Arrays.sort(inputarray) sliding window with a size of 2. Check for the sum in the sliding window. subtract the start when window moves"See full answer

    Software Engineer
    Coding
    +5 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 

    "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"

    Mahaboob P. - "int[] sqSorted(int[] nums) { int i = 0, j = nums.length-1; int k = nums.length-1; int[] sqs = new int[nums.length]; while(i n1) { sqs[k--] = n2; j--; } else { sqs[k--] = n1; i++; } } for(int n: sqs) System.out.println(n); return sqs; }"See full answer

    Software Engineer
    Coding
    +2 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    Video answer for 'Merge k sorted linked lists.'
    +6

    "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"

    Guilherme F. - "A much better solution than the one in the article, below: It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods. shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns. My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N)) class ListNode { constructor(val = 0, next = null) { th"See full answer

    Software Engineer
    Coding
    +4 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

    Software Engineer
    Coding
    +2 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +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
    Coding
    +4 more
  • Meta (Facebook) logoAsked at Meta (Facebook) 
    +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
    Coding
    +2 more
Showing 1-20 of 28