Facebook Interview Question for Solutions Engineers


Country: Israel
Interview Type: Phone Interview




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

You can implement this in O(K) time and space. The approach I did was store an array of size K with all values set to 0. This will store the index for the K-th list.

The hasNext() function can be implemented by iterating through K-lists and checking if at least one of the indices is less than N. The next() function can be implemented by iterating through the K indices, and taking the index with a minimum value. The index with the minimum value is then incremented by one.

class MyIterator:
  def __init__(self, lists):
    self.lists = lists
    self.listCount = len(lists)
    self.listLen = len(lists[0])

    self.indices = [0] * self.listCount

  def next(self):
    val = float('inf')
    idx = -1

    for i in range(self.listCount):
      index = self.indices[i]
      if index < self.listLen:
        current_val = self.lists[i][index]
        if current_val < val:
          val = current_val
          idx = i

    if idx >= 0:
      self.indices[idx] += 1

    return val

  def hasNext(self):
    for i in range(self.listCount):
      if self.indices[i] < self.listLen:
        return True
    return False

lists = [[1,5,7], [2,3,10],[4,6,9]]
it = MyIterator(lists)
while it.hasNext():
  print(it.next())

- Anonymous January 14, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MyIterator{
PriorityQueue<Integer> pq=new PriorityQueue<>();
int [][]data=null;
int []k=null;
public MyIterator(int [][] data) {
this.data=data;
k=new int[data.length];
this.preProcess(data);
}
int next(){
int tmp=pq.remove();
this.postProcess(tmp);
return tmp;

}
boolean hashNext(){
return pq.isEmpty();

}
public void preProcess(int [][]data){
for (int i = 0; i < k.length; i++) {
pq.add(data[i][k[i]]);
}
}
public void postProcess(int removedData){
for (int i = 0; i < k.length; i++) {
if(k[i]<data[i].length && data[i][k[i]]==removedData){
k[i]=k[i]+1;
if(k[i]<data[i].length){
pq.add(data[i][k[i]]);
}
return;
}
}
}
}
public class IteratorInKSortedArray {

public static void main(String[] args) {
int [][]in={
{1,5,7},
{2,3,10},
{4,6,9}
};


MyIterator itr=new MyIterator(in);
while(!itr.hashNext()){
System.out.print(itr.next()+" ");
}

}

}

- Achyut January 15, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MyIterator{
PriorityQueue<Integer> pq=new PriorityQueue<>();
int [][]data=null;
int []k=null;
public MyIterator(int [][] data) {
this.data=data;
k=new int[data.length];
this.preProcess(data);}
int next(){
int tmp=pq.remove();
this.postProcess(tmp);
return tmp;}
boolean hashNext(){
return pq.isEmpty();

}
public void preProcess(int [][]data){
for (int i = 0; i < k.length; i++) {
pq.add(data[i][k[i]]);
}}
public void postProcess(int removedData){
for (int i = 0; i < k.length; i++) {
if(k[i]<data[i].length && data[i][k[i]]==removedData){
k[i]=k[i]+1;
if(k[i]<data[i].length){
pq.add(data[i][k[i]]);
}
return;}}}}
public class IteratorInKSortedArray {

public static void main(String[] args) {
int [][]in={
{1,5,7},
{2,3,10},
{4,6,9}
};
MyIterator itr=new MyIterator(in);
while(!itr.hashNext()){
System.out.print(itr.next()+" ");
}}}

- Anonymous January 15, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use K length array and priority queue
class MyIterator{
PriorityQueue<Integer> pq=new PriorityQueue<>();
int [][]data=null;
int []k=null;
public MyIterator(int [][] data) {
this.data=data;
k=new int[data.length];
this.preProcess(data);
}
int next(){
int tmp=pq.remove();
this.postProcess(tmp);
return tmp;}
boolean hashNext(){
return pq.isEmpty();}
public void preProcess(int [][]data){
for (int i = 0; i < k.length; i++) {
pq.add(data[i][k[i]]);
}}
public void postProcess(int removedData){
for (int i = 0; i < k.length; i++) {
if(k[i]<data[i].length && data[i][k[i]]==removedData){
k[i]=k[i]+1;
if(k[i]<data[i].length){
pq.add(data[i][k[i]]);
}
return;}}}

- Achyut January 15, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use k length array and priority queue
in hasNext() - return queue is empty or not
in next() - remove from queue and increase index value from k length array accordingly

- Achyut January 15, 2020 | Flag Reply


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