


20+ Facebook Software Developer Interview Questions and Answers
Q1. Saving Money Problem Statement
Ninja is adventurous and loves traveling while being mindful of his expenses. Given a set of 'N' stations connected by 'M' trains, each train starting from station 'A' and reachin...read more
The task is to find the cheapest price from the given source to destination with up to K stops.
Read the number of test cases
For each test case, read the number of stations and trains
Read the details of each train (source, destination, ticket price)
Read the source station, destination station, and maximum number of stops
Implement a graph data structure to represent the stations and trains
Use a modified version of Dijkstra's algorithm to find the cheapest price with up to K sto...read more
Q2. Longest Increasing Path in a 2D Matrix Problem Statement
Given a matrix of non-negative integers of size 'N x M', where 'N' and 'M' denote the number of rows and columns respectively, find the length of the lon...read more
The task is to find the length of the longest increasing path in a 2D matrix, where you can move in four directions: left, right, up, or down from each cell.
Traverse the matrix and for each cell, find the longest increasing path starting from that cell
Use dynamic programming to store the length of the longest increasing path for each cell
Recursively explore all four directions from each cell, checking if the next cell is greater than the current cell
Keep track of the maximum ...read more
Q3. Binary Tree Construction from Preorder and Inorder Traversal
The goal is to construct a binary tree from given preorder and inorder traversal lists of the tree nodes.
Example:
Input:
preorder = [1, 2, 4, 7, 3]
i...read more
The task is to construct a binary tree using the given inorder and preorder traversals.
Use the preorder traversal to determine the root of the binary tree
Use the inorder traversal to determine the left and right subtrees of the root
Recursively construct the left and right subtrees
Return the root node of the constructed binary tree
Q4. Combination Sum Problem Statement
Given an array of distinct positive integers ARR
and a non-negative integer 'B', find all unique combinations in the array where the sum is equal to 'B'. Numbers can be chosen ...read more
The task is to find all unique combinations in an array whose sum is equal to a given target sum.
Use backtracking to generate all possible combinations
Sort the array in non-decreasing order to ensure elements in each combination are in non-decreasing order
Start with an empty combination and iterate through the array, adding each element to the combination and recursively calling the function with the remaining sum
If the sum becomes zero, add the combination to the result
If th...read more
Q5. Merging Accounts Problem
Given a list ACCOUNTS
where each element consists of a list of strings, with the first element being the name of the account holder, and the subsequent elements being the email addresse...read more
The task is to merge accounts belonging to the same person based on common emails and return the merged accounts.
Iterate through each account and create a mapping of emails to account holders
Iterate through the mapping and merge accounts with common emails
Sort the merged accounts and return the result
Q6. K - Sum Path In A Binary Tree
Given a binary tree where each node contains an integer value, and a value 'K', your task is to find all the paths in the binary tree such that the sum of the node values in each p...read more
The task is to print every path of a binary tree with the sum of nodes in the path as 'K'.
Traverse the binary tree and keep track of the current path and its sum
At each node, check if the sum of the current path equals 'K'
If yes, add the current path to the result
Continue traversing the left and right subtrees recursively
Remove the current node from the path before backtracking
Q7. Path Counting in Directed Graph
Given a directed graph with a specified number of vertices V
and edges E
, your task is to calculate the total number of distinct paths from a given source node S
to all other no...read more
Calculate the total number of distinct paths from a given source node to all other nodes in a directed graph.
Use dynamic programming to keep track of the number of paths from the source node to each node in the graph.
Consider using modular arithmetic to handle large numbers and prevent overflow.
Start by initializing the number of paths from the source node to itself as 1.
Iterate through the edges of the graph and update the number of paths for each destination node.
Return the...read more
Q8. Longest Route Problem Statement
Given a 2-dimensional binary matrix called Mat
of size N x M that consists solely of 0s and 1s, find the length of the longest path from a specified source cell to a destination ...read more
Find the length of the longest path from a source cell to a destination cell in a binary matrix.
Use depth-first search (DFS) to explore all possible paths from source to destination.
Keep track of visited cells to avoid revisiting them.
Return the length of the longest path found, or -1 if no path exists.
Q9. Course Schedule II Problem Statement
You are provided with a number of courses 'N', some of which have prerequisites. There is a matrix named 'PREREQUISITES' of size 'M' x 2. This matrix indicates that for ever...read more
Given courses with prerequisites, determine a valid order to complete all courses.
Use topological sorting to find a valid order of courses.
Create a graph with courses as nodes and prerequisites as edges.
Start with courses that have no prerequisites and remove them from the graph.
Continue this process until all courses are taken or there are no valid courses left.
If there is a cycle in the graph, it is impossible to complete all courses.
Q10. Merge Intervals Problem Statement
You are provided with 'N' intervals, each containing two integers denoting the start time and end time of the interval.
Your task is to merge all overlapping intervals and retu...read more
Merge overlapping intervals and return sorted list of merged intervals.
Sort the intervals based on start times.
Iterate through intervals and merge overlapping intervals.
Return the merged intervals in sorted order.
Q11. Search In Rotated Sorted Array Problem Statement
Given a rotated sorted array ARR
of size 'N' and an integer 'K', determine the index at which 'K' is present in the array.
Note:
1. If 'K' is not present in ARR,...read more
Given a rotated sorted array, find the index of a given integer 'K'.
Use binary search to find the pivot point where the array is rotated.
Then perform binary search on the appropriate half of the array to find 'K'.
Handle cases where 'K' is not present in the array by returning -1.
Q12. Rank from Stream Problem Statement
Given an array of integers ARR
and an integer K
, determine the rank of the element ARR[K]
.
Explanation:
The rank of any element in ARR
is defined as the number of elements sma...read more
Given an array and an index, find the number of elements smaller than the element at that index appearing before it in the array.
Iterate through the array up to index K and count the number of elements smaller than ARR[K].
Return the count as the rank of ARR[K].
Handle edge cases like empty array or invalid index K.
Q13. LRU Cache Design Question
Design a data structure for a Least Recently Used (LRU) cache that supports the following operations:
1. get(key)
- Return the value of the key if it exists in the cache; otherwise, re...read more
Design a Least Recently Used (LRU) cache data structure that supports get and put operations with capacity constraint.
Implement a doubly linked list to maintain the order of recently used keys.
Use a hashmap to store key-value pairs for quick access.
Update the order of keys in the linked list on get and put operations.
Evict the least recently used key when the cache reaches its capacity.
Q14. Power Set Generation
Given a sorted array of 'N' integers, your task is to generate the power set for this array. Each subset of this power set should be individually sorted.
A power set of a set 'ARR' is the s...read more
Generate the power set of a sorted array of integers with individually sorted subsets.
Use recursion to generate all possible subsets by including or excluding each element in the array.
Sort each subset before adding it to the power set.
Handle base cases for empty array and single element array.
Ensure the subsets are unique by using a set data structure.
Time complexity can be exponential due to the nature of generating all subsets.
Q15. K Closest Points to Origin Problem Statement
Your house is located at the origin (0,0) of a 2-D plane. There are N
neighbors living at different points on the plane. Your goal is to visit exactly K
neighbors wh...read more
Find the K closest points to the origin in a 2-D plane using Euclidean Distance.
Calculate the Euclidean Distance of each point from the origin
Sort the points based on their distances
Return the first K points as the closest neighbors
Q16. Roman Numeral to Integer Conversion
Convert a string representing a Roman numeral into its integer equivalent and return the result.
Explanation:
Roman numerals are represented by seven different symbols: I, V,...read more
Convert a Roman numeral string to its integer equivalent.
Create a mapping of Roman numeral symbols to their integer values.
Iterate through the input string and add the corresponding integer values.
Handle cases where subtraction is needed (e.g., IV = 4, IX = 9).
Q17. Longest Increasing Subsequence Problem Statement
Given an array of integers with 'N' elements, determine the length of the longest subsequence where each element is greater than the previous element. This subse...read more
Find the length of the longest strictly increasing subsequence in an array of integers.
Use dynamic programming to solve this problem efficiently.
Initialize an array to store the length of the longest increasing subsequence ending at each index.
Iterate through the array and update the length of the longest increasing subsequence for each element.
Return the maximum value in the array as the result.
Q18. Pair Sum Problem Statement
You are given an integer array 'ARR' of size 'N' and an integer 'S'. Your task is to find and return a list of all pairs of elements where each sum of a pair equals 'S'.
Note:
Each pa...read more
Find pairs of elements in an array that sum up to a given value, sorted in a specific order.
Iterate through the array and for each element, check if the complement (S - current element) exists in a hash set.
If the complement exists, add the pair to the result list.
Sort the result list based on the criteria mentioned in the question.
Return the sorted list of pairs.
Q19. Arithmetic Expression Evaluation Problem Statement
You are provided with a string expression
consisting of characters '+', '-', '*', '/', '(', ')' and digits '0' to '9', representing an arithmetic expression in...read more
Evaluate arithmetic expressions in infix notation with given operators and precedence rules.
Parse the infix expression to postfix using a stack.
Evaluate the postfix expression using a stack.
Handle operator precedence and parentheses while evaluating.
Ensure no division by zero cases and operands fit in 32-bit integer.
Q20. Remove Duplicates from Sorted Array Problem Statement
You are given a sorted integer array ARR
of size N
. Your task is to remove the duplicates in such a way that each element appears only once. The output shou...read more
Remove duplicates from a sorted array in-place with O(1) extra memory.
Use two pointers - one for iterating through the array and another for placing unique elements.
Compare current element with next element to identify duplicates and skip them.
Update array in-place by moving unique elements to the front.
Return the length of the array after removal of duplicates.
Facebook stores likes/dislikes using a combination of databases and caching systems.
Likes/dislikes are stored in a distributed database system like Cassandra or HBase.
Each like/dislike is associated with a user and the content being liked/disliked.
The database is sharded to handle the large volume of likes/dislikes.
Caching systems like Memcached or Redis are used to improve read performance.
Likes/dislikes can be stored as separate entities or as counters depending on the use ...read more
Facebook implements graph search by indexing user connections and content to enable efficient search queries.
Facebook indexes user connections and content to build a graph database.
The graph database is used to store and retrieve information about users, their relationships, and their content.
Graph search queries are executed by traversing the graph database to find relevant connections and content.
Facebook uses various algorithms and optimizations to improve the efficiency a...read more
Facebook Chat is a real-time messaging system that allows users to send and receive instant messages.
Facebook Chat uses a client-server architecture.
It utilizes long polling or WebSockets for real-time communication.
Messages are stored in a message queue for delivery.
Chat messages are encrypted for security.
Facebook Chat supports features like read receipts, typing indicators, and group chats.
Demand paging is a memory management technique where pages are loaded into memory only when needed.
Demand paging allows for efficient memory utilization by loading pages into memory on demand.
It reduces the amount of initial memory required to start a process.
When a page is needed but not in memory, a page fault occurs and the required page is loaded from disk.
Demand paging allows for larger virtual memory space than physical memory.
Examples of demand paging systems include W...read more
Hadoop is a framework for distributed storage and processing of large data sets.
Hadoop is used for storing and processing big data across a distributed network of computers.
It is based on the MapReduce programming model, which allows for parallel processing of data.
Hadoop consists of HDFS for storage and YARN for resource management.
It is used for tasks like data warehousing, log processing, recommendation systems, and more.
Popular tools in the Hadoop ecosystem include Apache...read more
Interview Process at Facebook Software Developer

Top Software Developer Interview Questions from Similar Companies







Reviews
Interviews
Salaries
Users/Month

