• 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

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

Flipkart SDET Interview Experience

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Apriori algorithm C Code Data Mining

Client Server C program

Calculate price of parking from parking start end time prices

SAP Off Campus Hiring_ March 2015 Verbal Skills

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

Handle duplicates in Binary Search Tree

Sequence Finder Dynamic Programming

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

Circular Linked List

Password Predictor

CodeChef Code SGARDEN

DFS (Depth First Search)

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

Number of Islands BFS/DFS

Find if two rectangles overlap

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

Test Cases for Round Function

Printing Longest Common Subsequence

Leetcode: Edit Distance

strtok()

Find position of the only set bit

Maximum of all subarrays of size k

simple sql injection

Implement LRU Cache

Memory Efficient LinkedList

How strtok() Works

Implement a generic binary search algorithm for Integer Double String etc

Copyright © 2023 · Genesis Framework · WordPress · Log in