• 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

Apriori algorithm C Code Data Mining

Sort Stack in place

Count number of ways to reach a given score in a game

Leetcode: Merge Intervals

Given a string, find the first character which is non-repetitive

Binary Tree in Java

Possible sizes of bus to carry n groups of friends

Find two non repeating elements in an array of repeating elements

Doubly linked list

Generate largest number arranging a no. of given non negative integer numbers

Serialise Deserialise N-ary Tree

Print Power Set of a Set

SAP Hiring Off-Campus General Aptitude

BFS (Breath First Search)

System Design: Designing a LLD for Hotel Booking

25 horses 5 tracks Find 3 fastest puzzle

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

Stickler thief

Sort an array according to the order defined by another array

Code Chef PRGIFT Solution

Implement a generic binary search algorithm for Integer Double String etc

LeetCode : Word Search

Given a float number convert it into the string WITHOUT using any inbuilt Function

Find and print longest consecutive number sequence in a given sequence in O(n)

Add Sub Multiply very large number stored as string

Daughter’s Age VeryGood Puzzle

Naurki.com Security Breach

Find if a binary tree is height balanced ?

Hackerearth : Counting Subarrays

Walmart Labs Interview Experience

Copyright © 2025 · Genesis Framework · WordPress · Log in