• 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

Common Ancestor in a Binary Tree or Binary Search Tree

Find Pythagorean Triplets in an array in O(N)

Find Percentage of Words matching in Two Strings

Generic Object Oriented Stack with Template

Flipkart SDET Interview Experience

Subset Sum Problem Dynamic programming

Python Array String

Find if a binary tree is height balanced ?

Sort Stack in place

Find position of the only set bit

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Reversal of LinkedList

Find the number ABCD such that when multipled by 4 gives DCBA.

Singly linked list

Find the element that appears once others appears thrice

Print Power Set of a Set

CodeChef’ RRCOPY

TicTacToe Game As Asked in Flipkart

Find min element in Sorted Rotated Array (Without Duplicates)

Walmart Labs Interview Experience

Length of the longest substring without repeating characters

K’th Largest Element in BST when modification to BST is not allowed

Convert number to words java

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

Sequence Finder Dynamic Programming

Best Java Book | Top Java Programming Book for Beginners

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

SAP Hiring Off-Campus General Aptitude

Possible sizes of bus to carry n groups of friends

VMWare Openings

Copyright © 2025 · Genesis Framework · WordPress · Log in