


20+ Facebook Interview Questions and Answers for Freshers
Q1. Base 58 Conversion Problem Statement
You are given a decimal number 'N'. Your task is to convert this number into a base 58 representation.
The Base58 alphabet is defined by the following characters: “123456789...read more
Convert a decimal number to base 58 using a specific alphabet.
Iterate through each test case and convert the decimal number to base 58 using the given alphabet.
Handle the conversion by dividing the number by 58 and taking the remainder to find the corresponding base 58 character.
Build the base 58 representation by appending the characters in reverse order.
Output the final base 58 representation for each test case.
Q2. Interleaving Two Strings Problem Statement
You are given three strings 'A', 'B', and 'C'. Determine if 'C' is formed by interleaving 'A' and 'B'.
String 'C' is an interleaving of 'A' and 'B' if the length of 'C...read more
Q3. 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
Q4. 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
Q5. Arithmetic Progression Queries Problem Statement
Given an integer array ARR
of size N
, perform the following operations:
- update(l, r, val):
Add (val + i)
to arr[l + i]
for all 0 ≤ i ≤ r - l
.
- rangeSum(l, r):...read more
Q6. Bird and Maximum Fruit-Gathering Problem Statement
A ninja bird can gather fruits from trees arranged in a circle. Each tree has an associated fruit value. The bird can gather all the fruits from a tree in 0.5 ...read more
The task is to find the maximum number of fruits a bird can gather within a given time frame, moving between adjacent trees.
Start from each tree and calculate the maximum fruits that can be gathered within the time frame.
Use a sliding window approach to keep track of the fruits collected while moving between trees.
Choose the starting tree that gives the maximum total fruits collected.
Example: For input N=7, M=3, ARR={2, 1, 3, 5, 0, 1, 4}, the output is 9 by starting from tree...read more
Q7. Triplets with Given Sum Problem
Given an array or list ARR
consisting of N
integers, your task is to identify all distinct triplets within the array that sum up to a specified number K
.
Explanation:
A triplet i...read more
The task is to find all distinct triplets in an array that sum up to a specified number.
Iterate through the array and use nested loops to find all possible triplets.
Use a set to store unique triplets and check if the sum equals the target sum.
Handle edge cases like duplicate elements and no valid triplets.
Return the triplets or -1 if no valid triplet exists.
Q8. Get DFS Path Problem Statement
Given an undirected graph represented by vertices and edges, identify and return a path between two specified vertices using Depth First Search (DFS). The path should be presented...read more
Q9. 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.
Q10. Pair Sum Problem Statement
You are provided with an array ARR
consisting of N
distinct integers in ascending order and an integer TARGET
. Your objective is to count all the distinct pairs in ARR
whose sum equal...read more
Count distinct pairs in an array whose sum equals a given target.
Use two pointers approach to iterate through the array and find pairs with sum equal to target.
Keep track of visited pairs to avoid counting duplicates.
Return -1 if no such pair exists with the given target.
Q11. All Prime Numbers Less Than or Equal to N
Given a positive integer N
, your task is to return all the prime numbers less than or equal to N
.
Note:
1) A prime number is a number that has only two factors: 1 and t...read more
Return all prime numbers less than or equal to a given positive integer N.
Iterate from 2 to N and check if each number is prime using a helper function.
A number is prime if it has only 2 factors: 1 and itself.
Optimize by checking divisibility only up to square root of the number.
Q12. 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.
Q13. 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.
Q14. 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.
Q15. 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.
Q16. 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.
Q17. Running Absolute Difference in Arrays
Given an array ARR
consisting of 'N' non-negative integers, compute the running absolute difference of elements at even and odd index positions, respectively.
Input:
An int...read more
Compute running absolute difference of elements at even and odd index positions in an array.
Iterate through the array and calculate absolute difference between elements at even and odd indices.
Keep track of running absolute differences for even and odd indices separately.
Output the final running absolute differences for even and odd indices.
Q18. 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.
Q19. What is the best way to keep man alive
The best way to keep a man alive is to ensure he has access to basic necessities like food, water, shelter, and medical care.
Provide access to clean drinking water
Ensure a balanced and nutritious diet
Provide adequate shelter and protection from the elements
Ensure access to medical care and treatment
Encourage regular exercise and physical activity
Q20. how you can be good analyser data
To be a good data analyzer, one needs to have strong analytical skills and attention to detail.
Develop strong analytical skills through practice and training
Pay attention to details and look for patterns in the data
Use tools and software to help with data analysis
Stay up-to-date with industry trends and best practices
Collaborate with others to gain different perspectives on the data
Validate and verify data to ensure accuracy
Communicate findings clearly and effectively
Q21. How an elephant die with ant
It's impossible for an elephant to die from an ant.
Ants are too small to cause any harm to an elephant.
Elephants have thick skin which protects them from insect bites.
Even if an elephant accidentally ingests an ant, it won't cause any harm.
This question is likely a trick or joke question.
Top HR Questions asked in Facebook for Freshers
Interview Process at Facebook for Freshers

Top Interview Questions from Similar Companies








Reviews
Interviews
Salaries
Users/Month

