Facebook interview experience Real time questions & tips from candidates to crack your interview

SDE - 1

Facebook
upvote
share-icon
4 rounds | 8 Coding problems

Interview preparation journey

expand-icon
Preparation
Duration: 4 Months
Topics: Data Structures, Algorithms, System Design, Aptitude, OOPS
Tip
Tip

Tip 1 : Must do Previously asked Interview as well as Online Test Questions.
Tip 2 : Go through all the previous interview experiences from Codestudio and Leetcode.
Tip 3 : Do at-least 2 good projects and you must know every bit of them.

Application process
Where: Campus
Eligibility: Above 7 CGPA
Resume Tip
Resume tip

Tip 1 : Have at-least 2 good projects explained in short with all important points covered.
Tip 2 : Every skill must be mentioned.
Tip 3 : Focus on skills, projects and experiences more.

Interview rounds

01
Round
Hard
Online Coding Test
Duration90 minutes
Interview date11 Jul 2019
Coding problem2

This was an online coding round where we were supposed to solve 2 questions under 90 minutes . Both the questions in my set were related to Graphs and were quite tricky and heavy to implement.

1. Count Ways

Easy
24m average time
81% success
0/40
Asked in companies
GoogleAmazonFacebook

You have been given a directed graph of 'V' vertices and 'E' edges.

Your task is to count the total number of ways to reach different nodes in the graph from the given source node ‘S’. A way is called as different from others if the destination node or used edges differ.

As the total number of such ways can be large, print the total ways modulo 10 ^ 9 + 7.

Note:

1. Include the source node as a destination also.
2. There are no cycles in the given graph.
Problem approach

Steps: 

1) Create a recursive function that takes index of node of a graph and the destination index. Keep a global or a static variable count to store the count. Keep a record of the nodes visited in the current path by passing a visited array by value (instead of reference, which would not be limited to the current path).
2) If the current nodes is the destination increase the count.
3) Else for all the adjacent nodes, i.e. nodes that are accessible from the current node, call the recursive function with the index of adjacent node and the destination.
4) Print the Count.

Try solving now

2. Course Schedule II

Hard
50m average time
50% success
0/120
Asked in companies
AppleUberPhonePe

You have been given ‘N’ courses and some courses may have prerequisites. Now consider a matrix ‘PREREQUISITES’ of size 'M' x 2 which represents that you must complete the course 'PREREQUISITES[i][1]' before the course 'PREREQUISITES[i][0]'.


Your task is to return the order of courses you should take to finish all courses.


Note:
If it is impossible to finish all courses, return an empty array. If there are multiple answers, return any one.


For example:
Input:
3 2
1 2
2 3

There are three courses to take. To start with, First course 3 is taken. Then course 2 is taken for which course 3 must be completed. 

At last course 1 is taken for which course 2 must be completed. So the correct course order is [3,2,1].    
Problem approach

Approach : This problem was based on Topological Sorting .

The first node in the topological ordering will be the node that doesn't have any incoming edges. Essentially, any node that has an in-degree of 0 can start the topologically sorted order. If there are multiple such nodes, their relative order doesn't matter and they can appear in any order.

Algorithm : 

1) Initialize a queue, Q to keep a track of all the nodes in the graph with 0 in-degree.
2) Iterate over all the edges in the input and create an adjacency list and also a map of node v/s in-degree.
3) Add all the nodes with 0 in-degree to Q.
4) The following steps are to be done until the Q becomes empty.
4.1) Pop a node from the Q. Let's call this node, N.
4.2) For all the neighbors of this node, N, reduce their in-degree by 1. If any of the nodes' in- 
degree reaches 0, add it to the Q.
4.3) Add the node N to the list maintaining topologically sorted order.
4.4) Continue from step 4.1.

Try solving now
02
Round
Medium
Face to Face
Duration60 Minutes
Interview date11 Jul 2019
Coding problem2

This was a Data Structures and Algorithms round with some standard questions . I was expected to come up with an
efficient approach and code it as well .

1. Merge Intervals

Moderate
20m average time
80% success
0/80
Asked in companies
CultfitHCL TechnologiesUber

You are given N number of intervals, where each interval contains two integers denoting the start time and the end time for the interval.

The task is to merge all the overlapping intervals and return the list of merged intervals sorted by increasing order of their start time.

Two intervals [A,B] and [C,D] are said to be overlapping with each other if there is at least one integer that is covered by both of them.

For example:

For the given 5 intervals - [1, 4], [3, 5], [6, 8], [10, 12], [8, 9].

Since intervals [1, 4] and [3, 5] overlap with each other, we will merge them into a single interval as [1, 5].

Similarly, [6, 8] and [8, 9] overlap, merge them into [6,9].

Interval [10, 12] does not overlap with any interval.

Final List after merging overlapping intervals: [1, 5], [6, 9], [10, 12].
Problem approach

Steps : 

1) First, we sort the list as described. 
2) Then, we insert the first interval into our merged list and continue considering each interval in turn as follows - 
2.1) If the current interval begins after the previous interval ends, then they do not overlap and we can 
append the current interval to merged. 
2.2) Otherwise, they do overlap, and we merge them by updating the end of the previous interval if it is 
less than the end of the current interval.

TC : O(N*log(N))
SC : O(1)

Follow Up : Prove your Greedy solution .

A simple proof by contradiction shows that this algorithm always produces the correct answer. First, suppose that the algorithm at some point fails to merge two intervals that should be merged. This would imply that there exists some triple of indices i, j, and k in a list of intervals ints such that i < j < k and (ints[i],ints[k]) can be merged, but neither (ints[i],ints[j]) nor (ints[j],ints[k]) can be merged . We have following inequalities from this scenario : 

ints[i].endints[j].endints[i].end≥ints[k].start



We can chain these inequalities (along with the following inequality, implied by the well-formedness of the intervals: ints[j].start <= ints[j].end ) to demonstrate a contradiction:


ints[i].end < ints[j].start ≤ ints[j].end < ints[k].start
ints[i].end ≥ ints[k].start



Therefore, all mergeable intervals must occur in a contiguous run of the sorted list.

Try solving now

2. Longest Route

Moderate
15m average time
85% success
0/80
Asked in companies
HSBCFacebookTata 1mg

You are given a 2-D binary matrix "Mat" of dimensions N x M consisting only of 0s and 1s. The cell consisting of 0 means that the cell is blocked and it cannot be visited whereas a cell with 1 as a value means it can be visited.

You are given a source cell and a destination cell. You need to find the length of the longest possible path from source to destination, given you can only move in 4 possible directions north(i.e from (i,j) to (i-1,j)), south(i.e from (i,j) to (i+1,j)), east(i.e from (i,j) to (i,j-1)), and west(i.e from (i,j) to (i,j+1)), and without visiting a cell twice.

Note:
1. If the move isn’t possible you remain in that cell only.
Problem approach
  1. The idea is to reduce the problem from finding a path from source to destination to finding a path from some node to destination and building our answer up.
  2. From the current cell, we look if there exists a neighbor from where I can reach the destination if no such neighbor exists we return -1.
  3. Else if a path exists we take the longest path then add 1 in it ( to account for the transition from the current cell to a neighbor).
  4. In the end, we change the visited current node to false and return the answer
Try solving now
03
Round
Medium
Face to Face
Duration50 Minutes
Interview date11 Jul 2019
Coding problem2

This was also a DSA round where I was asked to code only one of the questions but I eventually ended up coding both
as I had some spare time and explained my approches very smoothly to the interviewer . This round went preety well .

1. Longest Increasing Subsequence

Moderate
30m average time
65% success
0/80
Asked in companies
FacebookDisney + HotstarAmazon

For a given array with N elements, you need to find the length of the longest subsequence from the array such that all the elements of the subsequence are sorted in strictly increasing order.

Strictly Increasing Sequence is when each term in the sequence is larger than the preceding term.

For example:
[1, 2, 3, 4] is a strictly increasing array, while [2, 1, 4, 3] is not.
Problem approach

Approach 1 (Using DP ) :

This is a classic Dynamic Programming problem.
Steps : 
1) Let dp[i] is the longest increase subsequence which ends at nums[i] . 
2) For every i from 0 to n , traverse backwards from j=i-1 to j=0 and check if nums[i]>nums[j].
3) If nums[i]>nums[j] , update dp[i]=max(dp[i] , dp[j]+1)
4) Finally return the maximum element from the DP array

TC: O(N^2)
SC: O(N)

Approach 2 (Using Binary Search and Greedy ) :

Steps:
1) Maintain a vector v (this will remain sorted at all times) .
2) Push the first element into the vector v.
3) Run a loop from 1 to n and for every element check if nums[i]>v.back()
4) If nums[i]>v.back() , simply push nums[i] into v .
5) Else , find the lower_bound position of nums[i] in vector v and replace that element with nums[i] .
6) Finally , the length of the vector v gives us the length of the LIS.
7) To get the final sequence we can maintain a path vector where path[i] stores the index of the previous number in LIS


TC : O(N*Log(N))
SC : O(N)

Try solving now

2. Search In Rotated Sorted Array

Easy
12m average time
85% success
0/40
Asked in companies
Disney + HotstarArcesiumPhonePe

You have been given a sorted array/list 'arr' consisting of ‘n’ elements. You are also given an integer ‘k’.


Now the array is rotated at some pivot point unknown to you.


For example, if 'arr' = [ 1, 3, 5, 7, 8], then after rotating 'arr' at index 3, the array will be 'arr' = [7, 8, 1, 3, 5].


Now, your task is to find the index at which ‘k’ is present in 'arr'.


Note :
1. If ‘k’ is not present in 'arr', then print -1.
2. There are no duplicate elements present in 'arr'. 
3. 'arr' can be rotated only in the right direction.


Example:
Input: 'arr' = [12, 15, 18, 2, 4] , 'k' = 2

Output: 3

Explanation:
If 'arr' = [12, 15, 18, 2, 4] and 'k' = 2, then the position at which 'k' is present in the array is 3 (0-indexed).


Problem approach

This was a preety standard Binary Search Question and I had solved this question before on platforms like LeetCode and CodeStudio . I was asked this question to test my implementation skills and how well do I handle Edge Cases .

Approach :
1) The idea is to find the pivot point, divide the array in two sub-arrays and perform binary search.
2) The main idea for finding pivot is – for a sorted (in increasing order) and pivoted array, pivot element is the only element for which next element to it is smaller than it.
3) Using the above statement and binary search pivot can be found.
4) After the pivot is found out divide the array in two sub-arrays.
5) Now the individual sub – arrays are sorted so the element can be searched using Binary Search.

TC : O(Log(N))
SC : O(1)

Try solving now
04
Round
Medium
Face to Face
Duration50 Minutes
Interview date11 Jul 2019
Coding problem2

This was also a DSA round with 2 questions of Medium to Hard difficulty . I was expected to follow some clean code and OOPS principles to write the code in this round .

1. Rank from Stream

Moderate
15m average time
85% success
0/80
Asked in companies
SprinklrFacebookTata 1mg

You are given an array of ‘N’ integers say ‘ARR’ and provided with other integer say ‘K’. Now you have to find the Rank of ‘ARR[K]’.

Note:

The rank of any element in ‘ARR’ is equal to the number of smaller elements than ‘ARR[K]’ on its left side.

For example :

Input :
Let ‘ARR’’ = [6, 2, 9, 7] and ‘X’ = 3.
We can see there are two smaller elements than ‘ARR[3]’ = 7 on its left side
Output: 2

Follow Up :

Try to find rank in less time complexity than O(N), you may use some data structure to implement this.
Problem approach

Step 1- Making the ‘BST’ :

 

Let insert(TreeNode* <int> ROOT, int VAL) be a function which insert data into ‘BST’.

Now consider the following steps to implement the function :

  1. If ‘ROOT’ is NULL then make a new Node with data and return it.
  2. If 'VAL' is less than data of ‘ROOT’ then do:
    1. Make a recursive call with the left child of ‘ROOT’ as ‘ROOT’.
    2. Increase the size of the left subtree of ‘ROOT’ by 1.
  3. Otherwise do:
    1. Make a recursive call with the right child of ‘ROOT’ as ‘ROOT’.

Now iterate over the ‘ARR’ and feed vales in ‘ARR’ to insert to generate ‘BST’.

 

Step2 -Find Rank of ‘ARR[K]’ say ‘NUM’ from ‘BST’:

 

Let getRank(TreeNode* <int> ROOT, int NUM) be a function that returns the size of the left subtree of the node with the value ‘NUM’ or we can say Rank of ‘NUM’. 

 

Now consider the following steps to implement the function :

  1. If data at ‘ROOT’ is equal to ‘NUM’ then return the size of the left subtree.
  2. If data at ‘ROOT’ is less than ‘NUM’ then do:
    1. Make a recursive call with the left child of ‘ROOT’ as ‘ROOT’.
  3. If data at ‘ROOT’ is greater than ‘NUM’ then do:
    1. Let RIGHT_SIZE = Make a recursive call with the right child of ‘ROOT’ as ‘ROOT’.
    2. Return (size of the left subtree+RIGHT_SIZE+1).
Try solving now

2. LRU Cache Implementation

Moderate
25m average time
65% success
0/80
Asked in companies
CognizantArcesiumCisco

Design and implement a data structure for Least Recently Used (LRU) cache to support the following operations:

1. get(key) - Return the value of the key if the key exists in the cache, otherwise return -1.

2. put(key, value), Insert the value in the cache if the key is not already present or update the value of the given key if the key is already present. When the cache reaches its capacity, it should invalidate the least recently used item before inserting the new item.
You will be given ‘Q’ queries. Each query will belong to one of these two types:
Type 0: for get(key) operation.
Type 1: for put(key, value) operation.
Note :
1. The cache is initialized with a capacity (the maximum number of unique keys it can hold at a time).

2. Access to an item or key is defined as a get or a put operation on the key. The least recently used key is the one with the oldest access time.
Problem approach

Approach : 

Structure of an LRU Cache :

1) In practice, LRU cache is a kind of Queue — if an element is reaccessed, it goes to the end of the eviction order
2) This queue will have a specific capacity as the cache has a limited size. Whenever a new element is brought in, it
is added at the head of the queue. When eviction happens, it happens from the tail of the queue.
3) Hitting data in the cache must be done in constant time, which isn't possible in Queue! But, it is possible with
HashMap data structure
4) Removal of the least recently used element must be done in constant time, which means for the implementation of
Queue, we'll use DoublyLinkedList instead of SingleLinkedList or an array.

LRU Algorithm :
The LRU algorithm is pretty easy! If the key is present in HashMap, it's a cache hit; else, it's a cache miss.

We'll follow two steps after a cache miss occurs:

1) Add a new element in front of the list.
2) Add a new entry in HashMap and refer to the head of the list.

And, we'll do two steps after a cache hit:

1) Remove the hit element and add it in front of the list.
2) Update HashMap with a new reference to the front of the list.


Tip : This is a very frequently asked question in interview and its implementation also gets quite cumbersome if you
are doing it for the first time so better knock this question off before your SDE-interviews.

Try solving now

Here's your problem of the day

Solving this problem will increase your chance to get selected in this company

Skill covered: Programming

Which keyword is used for inheritance?

Choose another skill to practice
Similar interview experiences
company logo
SWE Intern
2 rounds | 5 problems
Interviewed by Facebook
1274 views
0 comments
0 upvotes
company logo
SDE - Intern
2 rounds | 3 problems
Interviewed by Facebook
1557 views
0 comments
0 upvotes
company logo
SDE - 1
5 rounds | 10 problems
Interviewed by Facebook
871 views
0 comments
0 upvotes
company logo
SDE - 1
4 rounds | 10 problems
Interviewed by Facebook
1257 views
0 comments
0 upvotes
Companies with similar interview experiences
company logo
SDE - 1
5 rounds | 12 problems
Interviewed by Amazon
113400 views
24 comments
0 upvotes
company logo
SDE - 1
4 rounds | 5 problems
Interviewed by Microsoft
56893 views
5 comments
0 upvotes
company logo
SDE - 1
3 rounds | 7 problems
Interviewed by Amazon
34497 views
6 comments
0 upvotes