• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Facebook Interview Question : Interleave List

May 14, 2018 by Dhaval Dave

Interleave List If input = [[1,2,3], [9, 0], [5], [-4,-5,-2,-3,-1]] then output = [1,9,5,-4,2,0,-5,3,-2,-3,-1];
Asked in Facebook F2F

Interleave List
Interleave List

In Python you can do it very easily

#!/usr/bin/python

max = 3
listofList = [[1, 2, 3], [9, 5], [-1], [12,90,3] ];
oplist =[];
for i in range(0,max):
    for list in listofList:
       if (i < len(list)):
           oplist.append(list[i])
print oplist

In Java using same logic..

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
import java.util.List;

class Ideone
{
	private static void interleaveLists(List<List<Integer>> ll){
    	if(ll == null)
    	    return;
    	int listLength = 0;
    	int total = 0;
    	for(List<Integer> list : ll){
	  if(list == null)
	    continue;
	  listLength = Math.max(list.size(), listLength);
	  total += list.size();
    	}
    	int[] result = new int[total];
    	int index = 0;
    	for(int i = 0; i < listLength; ++i){
    	  for(List<Integer> list : ll){
    	    if(list == null)
    	      continue;
                  if(i < list.size()){
    		result[index++] = list.get(i);
    		System.out.println(list.get(i));
    	      }
      	  }
    	}
    	for( int x :result){
    	   System.out.println(x);
    	}

    }
	public static <Integer> List<Integer> twoDArrayToList(int [][] twoDArray) {
	    List list = new ArrayList();
	    for (int [] array : twoDArray) {
	        list.add(Arrays.asList(array));
	    }
	    return list;
	}
	public static void main (String[] args) throws java.lang.Exception
	{	
		int [][] arr = {{1, 2, 3}, {9, 0}, {5}, {-4, -5, -2, -3, -1,5,2}};
		List<List<Integer>> ll = twoDArrayToList(arr);
		interleaveLists(ll);
	}
}

Similar Articles

Filed Under: Algorithm, Data Structure, Facebook Tagged With: 2d matrix, Array, Linked List

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find the kth number with prime factors 3, 5 and 7

Printing Longest Common Subsequence

Flipkart SDET Interview Experience

Find Percentage of Words matching in Two Strings

Get K Max and Delete K Max in stream of incoming integers

1014 Practice Question of New GRE – Princeton

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

FizzBuzz Solution C C++

Find position of the only set bit

simple sql injection

Urban Ladder Written Test.

SAP Hiring Off-Campus General Aptitude

Adobe Interview Questions 8 month Exp

Circular Linked List

Reverse a Linked List in groups of given size

Sort an array according to the order defined by another array

Number of Islands BFS/DFS

Level order traversal in Spiral form

Max Sum in circularly situated Values

C++ OOPs Part2

Generate next palindrome number

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Leetcode: Edit Distance

Knight Tour Problem (Graph – Breadth First Search)

Trie Dictionary

Check Binary Tree is Binary Search Tree or not

VMWare Openings

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Longest Increasing Subsequence

Find the element that appears once others appears thrice

Copyright © 2025 · Genesis Framework · WordPress · Log in