Interview Question

Software Engineer Interview

-Singapore

IBM

1. delete one block from a single linked list(can only use one pointer) 2. give a number of x, check if there are two elements in an array whose sum is equal to x, and improve your code

AnswerAdd Tags

Interview Answers

3 Answers

0

for(int i: aArr) should be: for (int i=0 ; i< aArr.length; i++)

Anonymous on

0

hashmap is not O(1), you need to iterate the array, so it is O(N)

Anonymous on

0

2. A. O(n2) -- add up all number pairs. B. O(1) -- 1. create an array bArr with same size of the given array and fill the value with: HashMap map = new HashMap(); for( int i : aArr) { bArr[i] = x - aArr[i]; if (map.containsKey(aArr)) map.put(aArr, 1) else map.put(aArr, 0) } for(int i =0 ; i 0) System.out.println(bArr[i] + " + " + aArr[i] + " is X"); }

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.