• 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

Coin Collection Dynamic Programming

Hackerearth : Counting Subarrays

Maximum difference between two elements s.t larger element appears after the smaller number

Advanced SQL Injection

Linked List V/S Binary Search Tree

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Sequence Finder Dynamic Programming

Search element in a matrix with all rows and columns in sorted order

Find the element that appears once others appears thrice

SAP Hiring Off-Campus General Aptitude

SAP Off Campus Hiring_ March 2015 Sample Questions

Interfaces in C++ (Abstract Classes in C++)

Templates in C++

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

Common Ancestor in a Binary Tree or Binary Search Tree

System Design: Designing a LLD for Hotel Booking

C++ OOPs Part2

Python Dictionaries

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

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

Maximum size of square sub matrix with all 1’s in a binary matrix

Minimum insertions to form a palindrome

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

BFS (Breath First Search)

Trapping Rain Water

The greedy coins game Dynamic Programming

Printing Longest Common Subsequence

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

Mirror of Tree

Level order traversal in Spiral form

Copyright © 2025 · Genesis Framework · WordPress · Log in