Booking.com Interview Question
Software EngineersCountry: Netherland
Interview Type: Phone Interview
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;
}
}
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;
}
}
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
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]);
}
}
}
}
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;
}
}
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 + " ");
}
}
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
}
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();
}
}
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)
}
}
}
- Anonymous February 11, 2016