Facebook Interview Question for SDE1s


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

We can just run DFS on a graph and check if vertices were already visited. If some vertex was already visited:
1) There is cycle in graph
2) Remove edge that make us a cycle (end with this visited vertex)
Because we visit each vertex only once, time complexity O(N), because we use hash to store visited vertices - space complexity is also O(N) (actually because we use recursion, it will always create extra variables on each level of recursion, so space complexity always not less then recursion depth, that can be also up to N in this case)
Code:

static class DirectedGraphNode { 
    int label; 
    List<DirectedGraphNode> neighbors; 
    
    DirectedGraphNode(int x) { 
      label = x; 
      neighbors = new ArrayList<>(); 
    } 
    
    List<Integer> getNeighborsLabels() {
      List<Integer> labels = new ArrayList<Integer>();
      for(DirectedGraphNode neighbor : neighbors)
        labels.add(neighbor.label);
      return labels;
    }
  } 

  private static boolean removeCycles(DirectedGraphNode node, Set<Integer> visited) {
    visited.add(node.label);
    boolean isCycle=false;
    Iterator<DirectedGraphNode> it=node.neighbors.iterator();
    while(it.hasNext()) {
      DirectedGraphNode neighbour = it.next();
      if(visited.contains(neighbour.label)) {
        isCycle=true;
        it.remove();
      } else {
        isCycle|=removeCycles(neighbour, visited);
      }
    }
    visited.remove(node.label);
    return isCycle;
  }

  private static void doTest1Yes() {
    DirectedGraphNode nodes[] = new DirectedGraphNode[3];
    for(int i=0;i<3;i++)
      nodes[i]=new DirectedGraphNode(i);
    nodes[2].neighbors.add(nodes[0]);
    nodes[1].neighbors.add(nodes[2]);
    nodes[0].neighbors.add(nodes[1]);
    
    System.out.println(removeCycles(nodes[0], new HashSet<Integer>())?"YES":"NO");
    for(int i=0;i<3;i++)
      System.out.println("Node "+i+": "+nodes[i].getNeighborsLabels());
  }

  private static void doTest2No() {
    DirectedGraphNode nodes[] = new DirectedGraphNode[5];
    for(int i=0;i<5;i++)
      nodes[i]=new DirectedGraphNode(i);
    nodes[0].neighbors.add(nodes[1]);
    nodes[0].neighbors.add(nodes[2]);
    nodes[1].neighbors.add(nodes[3]);
    nodes[1].neighbors.add(nodes[4]);
    
    System.out.println(removeCycles(nodes[0], new HashSet<Integer>())?"YES":"NO");
    for(int i=0;i<5;i++)
      System.out.println("Node "+i+": "+nodes[i].getNeighborsLabels());
  }
  
  private static void doTest3No() {
    DirectedGraphNode nodes[] = new DirectedGraphNode[4];
    for(int i=0;i<4;i++)
      nodes[i]=new DirectedGraphNode(i);
    nodes[0].neighbors.add(nodes[1]);
    nodes[0].neighbors.add(nodes[2]);
    nodes[1].neighbors.add(nodes[2]);
    nodes[1].neighbors.add(nodes[3]);
    nodes[3].neighbors.add(nodes[2]);
    
    System.out.println(removeCycles(nodes[0], new HashSet<Integer>())?"YES":"NO");
    for(int i=0;i<4;i++)
      System.out.println("Node "+i+": "+nodes[i].getNeighborsLabels());
  }

  private static void doTest4Yes() {
    DirectedGraphNode nodes[] = new DirectedGraphNode[4];
    for(int i=0;i<4;i++)
      nodes[i]=new DirectedGraphNode(i);
    nodes[0].neighbors.add(nodes[1]);
    nodes[0].neighbors.add(nodes[2]);
    nodes[1].neighbors.add(nodes[3]);
    nodes[2].neighbors.add(nodes[1]);
    nodes[3].neighbors.add(nodes[2]);
    
    System.out.println(removeCycles(nodes[0], new HashSet<Integer>())?"YES":"NO");
    for(int i=0;i<4;i++)
      System.out.println("Node "+i+": "+nodes[i].getNeighborsLabels());
  }

  public static void main(String[] args) {
    doTest1Yes();    
    doTest2No();    
    doTest3No();    
    doTest4Yes();    
  }
}

- Matviy Ilyashenko November 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
Pattern pattern = Pattern.compile("[74][47]+");
public String macthInteger(String input1[],String input2[]){
long grater=Long.parseLong(input2[0]);
int count=0;
int pointter=0;
boolean flag=false;
for(String jack:input2){
String finaloutput=input1[count];
long val=Long.parseLong(jack);
if(pattern.matcher(jack).matches() && val>=1 && val<=1000000000 && finaloutput.length()>=1 && finaloutput.length()<=10 && Pattern.matches("[a-zA-Z]+", finaloutput) == true){
flag=true;
if(grater>=val){
grater=val;
pointter=count;
}
}
count++;
}

if(flag==true){
return input1[pointter];
}else{
return "-1";
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Long n = in.nextLong();
Solution sol=new Solution();
String output="-1";
String s1="";
String s="";
if(n!=0 && n>=1 && n<=100000){
for(Long a0 = 0l; a0 < n; a0++){
s += in.next()+",";
Long n1 = in.nextLong();
s1+=""+n1+",";
}
if(!s.equals("") || !s1.equals("")){
String in1[]=s.split(",");
String in2[]=s1.split(",");
output=sol.macthInteger(in1,in2);
}
}
System.out.println(output);
in.close();
}
}

- Mohit Garg November 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More