Booking.com Interview Question for Software Engineers


Country: Netherland
Interview Type: Phone Interview




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

import java.util.ArrayList;

public class checksqrt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int[] a= {3,1,4,5,19,6};
		int[] b={14,9,22,36,8,0,64,25};
		ArrayList<Integer> c=new ArrayList<Integer>();
		
		
	for(int i=0;i<a.length;i++)
	{
		c.add(a[i]*a[i]);
	}
	
	for(int j=0;j<b.length;j++)
	{
	 if(c.contains(b[j]))
	 {
		System.out.println(b[j]); 
	 }
	}
}
	
}

- Anonymous February 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Effective Java solution using HashSet

import java.util.*;
import java.io.*;

public class Solution {

    public static void main(String[] args) {
        new Solution().solve();
    }

    public void solve() {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int[] a = {3, 1, 4, 5, 19, 6};
        int[] b = {14, 9, 22, 36, 8, 0, 64, 25};

        List<Integer> result = findSquares(a, b);

        for (int cur : result)
            out.println(cur);

        out.close();
    }

    // search a[i]^2 in b
    public List<Integer> findSquares(int[] a, int[] b) {
        List<Integer> result = new ArrayList<>();
        Set<Integer> squares = new HashSet<>();
        for (int i = 0; i < a.length; i++)
            squares.add(a[i] * a[i]);

        for (int i = 0; i < b.length; i++)
            if (squares.contains(b[i]))
                result.add(b[i]);

        return result;
    }
}

- megido December 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the second array and do binary search or want to optimize more check the lenght of both the array andsort the larger one and do binary search on it...

- chinmay.rakshit September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Insert 2nd array values into hash map
2) Iterate through 1st array and square each value
3) Check for squared value in hash map and print match

If you replace unordered_map ( find() runs O(n) time as worst case) with your own hash map and assume no collisions, this solution runs in O(n) time complexity.

void CheckSquares(int *a1, int *a2, int len1, int len2){

  std::unordered_map<int, bool> ht;
  int temp;
  int i;
  
  // 1) Insert 2nd array values into hash map
  for(i = 0; i < len2; i++)
    ht[ a2[i] ] = 1;
  
  
  // 2) Iterate through 1st array, square the values and print matches
  for(i = 0; i < len1; i++) {
    temp = pow(a1[i], 2);
    if( ht.find( temp ) != ht.end() )
	  std::cout << temp << std::endl;
  }
  

}

- rob September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findSquaresInArray(a1, a2):
	n1 = len(a1)
	n2 = len(a2)

	if n2 == 0 or n1 == 0:
		return None

	hashmap = {}
	results = []

	for el in a1:
		hashmap[el] = True

	for el in a2:
		root = pow(el, 0.5)
		int_root = int(root)
		if root != int_root:
			continue

		pos_root = hashmap.get(int_root)
		neg_root = hashmap.get(-1*int_root)

		if pos_root == True:
			results.append(el)

		elif neg_root == True:
			results.append(el)

		else:
			continue

	return results

- sauravmaximus September 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php

$a=[3,1,4,5,19,6];
$b=[14,9,22,36,8,0,64,25];

for($i=0;$i<count($a);$i++)
{
for($j=0;$j<count($b);$j++)
{
if(($a[$i]*$a[$i])==$b[$j])
{
echo $b[$j]."<br>";
}
}
}

?>

- Aaqib October 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

public class checksqrt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] a = { 3, 1, 4, 5, 19, 6 };
		int[] b = { 14, 9, 22, 36, 8, 0, 64, 25 };
		ArrayList<Integer> c = new ArrayList<Integer>();

		for (int i = 0; i < a.length; i++) {
			c.add(a[i] * a[i]);
		}

		for (int j = 0; j < b.length; j++) {
			if (c.contains(b[j])) {
				System.out.println(b[j]);
			}
		}
	}

}

- Anonymous February 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findSquare(a,b):
	return list(set(map(lambda x:x**2,a)) & set(b))

- rifat April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findSquare(a,b):
return list(set(map(lambda x:x**2,a)) & set(b))

- Anonymous April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findSquare(a,b):
return list(set(map(lambda x:x**2,a)) & set(b))

- rifat April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var arr = []
for (i in a, b) {
    var index = b.indexOf(Math.pow(a[i], 2))
    if (index > -1) {
        arr.push(b[index])
    }
}
return arr

- Anonymous January 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class Solution {
	public static void main (String args[]) {
		int [] array1 = new int[] {3,1, 4, 5, 19, 6}; 
		int [] array2 = new int[] {14, 9, 22, 36, 8, 0, 64, 25};
		List<Integer> list = new Solution().solve(array1, array2);
		for (int i : list) 
			System.out.println(i);
	}
	private Node root;
	public void add(int value) {
		add(root, value);
	}
	private void add(Node node, int value) {
		if (root == null) {
			root = new Node(value);
		}else{
			if (node.value > value) {
				if (node.left == null) {
					node.left= new Node(value);
				}else{
					add(node.left , value);
				}
			}else{
				if (node.right == null) {
					node.right = new Node(value);
				}else{
					add(node.right, value);
				}
			}
		}
	}
	private Node find(int value) {
		return find(root, value);
	}
	private Node find(Node node, int value) {
		if (node == null)
			return null;
		if (node.value == value) 
			return node;
		if (node.value > value)
			return find(node.left , value);
		return find(node.right, value);
	}
	public List<Integer> solve(int [] array1, int [] array2) {
		for (int i : array1) {
			add(i);
		}
		List<Integer> list = new LinkedList<>();
		for (int i : array2) {
			int sqare = (int) Math.sqrt(i);
			Node node = find(sqare);
			if (node != null && (sqare * sqare == i)){
				list.add(i);
			}
		}
		return list;
	}
}
class Node {
	int value;
	Node left, right;
	public Node(int value) {
		this.value = value;
		this.left = null;
		this.right= null;
	}
}

- w.kinaan July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findSquares()
	{
		int[] a = {3, 1, 4, 5, 19, 6};
		int[] c = {14, 9, 22, 36, 8, 0, 64, 25};
		int isit = 0;
		Set <Integer> b = new HashSet<Integer>();
		
		for(int i=0;i<c.length;i++)
		{
			b.add(c[i]);
		}
		
		for(int j=0;j<a.length;j++)
		{
			isit = a[j] * a[j];
			if(b.contains(isit))
				System.out.print(isit + " ");
		}
	}

- Nitesh August 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code in C#

public List<int> Result(int[] a,int[] b){
            List<int> res = new List<int>();
            HashSet<int> hs1 = new HashSet<int>(a);
            HashSet<int> hs2 = new HashSet<int>(b);

            foreach(var ele in hs2)
            {
                int sqrt = (int)Math.Sqrt(ele);
                if (sqrt * sqrt == ele && hs1.Contains(sqrt))
                    res.Add(ele);
            }
            return res
}

- Pranav August 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i in a:
        for j in b:
            if i**2 == j:
                print(j)

9
25
36

Its a linear solution, but it works

- neeraj_tiwar July 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i in a:
         for j in b:
             if i**2 == j:
                 print(j)

- rockytiwari125 July 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindSmartSubstring {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String subString = "Featuring stylish rooms and moornings for recreation boats,"
				+ " Room Mate Aitana is a designer hotel built in 2013 on an island in the IJ River in Amsterdam.";

		System.out.print(findSmartString(subString,30));

	}
	static String findSmartString(String s,int length) {
		String[] arr = s.split(" ");
		StringBuilder sb = new StringBuilder();
		int total = 0;
		for(String str:arr) {
			total = total + str.length();
			if(total > length)
				break;
		    sb.append(str+" ");
			
		}
		return sb.toString();
	}

}

- Tomas March 21, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Integer[] arr1 = {3, 1, 4, 5, 19, 6};
		Integer[] arr2 = {14, 9, 22, 36, 8, 0, 64, 25};
		
		HashSet<Integer> set = new HashSet<>();
		
		List<Integer> keyList = Arrays.asList(arr2);
		for(int i:arr1) {
			set.add(i * i);
		}	
		set.retainAll(keyList);
	
		for(int i:set) {
			System.out.println(i);
		}

- Tomas March 21, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = [3, 1, 4, 5, 19, 6]
b = [14, 9, 22, 36, 8, 0, 64, 25]

# O(n log n)
sorted(b)

l = filter(lambda x: x * x in b, a)
print((list(map(lambda x : x *x, list(l)))))

- Yaron P. May 04, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main

import (
	"fmt"
	"sort"
)

func findBinarySearch(elem int, array []int) bool {
	left := 0
	right := len(array) - 1
	for left <= right {
		mid := (left + right) / 2
		if elem == array[mid] {
			return true
		}
		if elem < array[mid] {
			right = mid - 1
		} else {
			left = mid + 1
		}
	}
	return false
}

func main() {
	a := []int{3, 1, 4, 5, 19, 6}
	b := []int{14, 9, 22, 36, 8, 0, 64, 25}

	sort.Ints(b)
	for _, e := range a {
		if findBinarySearch(e*e, b) {
			fmt.Println(e * e)
		}
	}
}

- dongre.avinash August 10, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<?php

	$a=[3,1,4,5,19,6];
	$b=[14,9,22,36,8,0,64,25];

     for($i=0;$i<count($a);$i++)
	 {
		   for($j=0;$j<count($b);$j++)
		   {
			   if(($a[$i]*$a[$i])==$b[$j])
			   {
				   echo  $b[$j]."<br>";
			   }
		   }
	 }

?>

- Aaqib October 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<?php

	$a=[3,1,4,5,19,6];
	$b=[14,9,22,36,8,0,64,25];

     for($i=0;$i<count($a);$i++)
	 {
		   for($j=0;$j<count($b);$j++)
		   {
			   if(($a[$i]*$a[$i])==$b[$j])
			   {
				   echo  $b[$j]."<br>";
			   }
		   }
	 }

?>

- Aaqib October 11, 2015 | 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