Directi Interview Question for SDE-2s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

function ij(A, B) {
"use strict"
        memo = []
        for (i = 0; i < A.length; i++) {
                for (j = i+1; j < B.length; j++) {
                        if (A[i] > B[j]) {
                                memo.push("" + (i +1) + "," + (j +1))
                        }
                }
        }
        var memo
        , i
        , j
        return memo
}
console.log(ij([1, 2, 3, 4], [5, 6, 2, 1]))

- srterpe September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

import sys
a=[int(x) for x in sys.stdin.readline().split('')]
b=[int(x) for x in sys.stdin.readline().split('')]
l=[]
for i in range(0,len(a)):
	for j in range(i+1,len(b)):
		if a[i]>b[j]:
			l.append((a[i],b[j]));
print l

- harshit.knit October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

+1 for doing it in `go`

- srterpe October 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in C#

public int[,] Pairs(int[] arrayOne, int[] arrayTwo)
        {
            List<int> list = new List<int>();
            for (int i = 0; i < arrayOne.Length; i++)
            {
                for (int j = i + 1; j < arrayTwo.Length; j++)
                {
                    if (arrayOne[i] > arrayTwo[j])
                    {
                        list.Add(arrayOne[i]);
                        list.Add(arrayTwo[j]);
                    }
                }
            }
            int listLenght = list.Count / 2;
            int[,] returnArray = new int[listLenght / 2, 2];
            for (int i = 0; i < listLenght; i += 2)
            {
                returnArray[i / 2, 0] = list[2 * i];
                returnArray[i / 2, 1] = list[(2 * i) + 1];
            }
            return returnArray;
        }

- k September 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I've misread the question
replace
"arrayOne[i] with i" and "arrayTwo[j] with j"

- k September 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in javascript.

{{
function solve(A, B) {
var ret = [];
for (var j = 0; j <= B.length; j++) {
for (var i = 0; (i < j && i < A.length); i++) {
if (A[i] < B[j]) {
ret.push({
i: i,
j: j
});
}
}
}
return ret;
}
}}

- Alex September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in javascript.

{{
function solve(A, B) {
var ret = [];
for (var j = 0; j <= B.length; j++) {
for (var i = 0; (i < j && i < A.length); i++) {
if (A[i] < B[j]) {
ret.push({
i: i,
j: j
});
}
}
}
return ret;
}
}}

- Alex September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in javascript.

function solve(A, B) { 
var ret = []; 
for (var j = 0; j <= B.length; j++) { 
for (var i = 0; (i < j && i < A.length); i++) { 
if (A[i] < B[j]) { 
ret.push({ 
i: i, 
j: j 
}); 
} 
} 
} 
return ret; 
}

- Alex September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution in java :

public class PairMatch {
	
	public static String [][] PairFind(int a[],int b[])
	{
		String c[][]=new String[a.length+b.length][a.length+b.length];
		
		for(int i=0;i<a.length;i++)
		{
			
			for(int j=i+1;j<b.length;j++)
			{
			
				if(a[i]>b[j])
				{
					c[i][j]=""+i+","+j;
				
				}
			}
		}
		return c;
	}

- deepjparekh September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution in java

public class Greater {
	
	public static void main(String[] args) {
		
		int a[]={10,02,30,51,45,89,12};
		int b[]={35,20,15,46,89,74,5};
		for (int i = 0; i < a.length; i++) {
			
			for (int j = 0; j < b.length; j++) {
				{
					if(a[i]>b[j]&&i<j)
					{
						System.out.println(i+" "+j);
						
						
					}
				}
				
			}
			
		}
	}

}

- achievers September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		
		int[] arr1 = { 1 ,6,4,2,6,3};
		int[] arr2 = {5,8,3,9,6,2};
		
		for(int i=0;i<arr1.length;i++) {
			
			for(int j = i+1;j<arr2.length;j++) {
				
				if(arr1[i]<arr2[j])
					System.out.println("("+i+","+j+")");
			}
		}
	}

- shukad333 September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

all of the i*j solutions are nice but if you sort the lists storing (index, value) you can then run a binary search to find where in the first array the value is smaller.

Nice thing about this is that now the solutions can build on each other.

- Anonymous September 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually you can't because we also need to check i<j for that. After sorting A[i]<B[j] condition can be checked by using 2 pointer method but i<j won't be in sequence.
Presently we have i<j in sequence(in sorted order) and A[i]<B[j].
Sorting both brings us to square 1 with an overhead of (nlogn+mlogm)

- geekycoder December 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public HashMap<Integer, Integer> Compare(int A[], int B[])	{
	HashMap<Integer, Integer> pairs = new HashMap<Integer, Integer>();
	for(int i=0; i<A.length; i++)	{
		for(int j=1; j<B.length; j++)	{
			if(A[i]>B[j])	{
				pairs.put(A[i], A[j]);
			}
		}
	}
	return pairs;
}

- jeevanus September 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main()
{
int a[] = {1,2,3,55,6,4,2,1,6,7,9};
int b[] = {1,4,22,5,6,7,8,9,0,5,3};
int n = sizeof(a)/sizeof(a[1]);
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i < j && a[i]>b[j])
cout<<"("<<i<<","<<j<<")"<<endl;
}
}

return 0;
}

- abhishekism September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary indexed tree would be the most efficient to solve this

- Anonymous October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary indexed tree would be the most efficient to solve this

- anonymous October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using binary indexed tree would be the most efficient to solve this

- anonymous October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static ArrayList<String> count(int[] a, int[] b){
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0; i<a.length; i++){
            for(int j=i+1; j<b.length; j++){
                if(a[i]>b[j])
                list.add(i+","+j);
            }
        }
        return list;
     }

- prynkdhk November 01, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More