Problems Flashcards
1
Q
Describe how you would combine two arrays where n is the length or the old array so that
result[i] == input[i]
result[i+n]==input[i]
And what is the time complexity?
A
- Create an ans array with space twice the size of the input array
- loop through the the length of the input array
2.1 And each time set
result[i] = result[i+1] = input[i]
2
Q
What does Bitwise XOR do and how do you write it in Java?
A
Copies the bit if it is set in one operand but not both.
^ is the operator used for XOR in Java
3
Q
How could you return the indices of 2 elements that add up to the sum?
A
- Create a HashMap
- compl = target - current
- check if compl is in map, if it is return the indices
- if compl not in map, add it to map
4
Q
How could you check if a list contains duplicates?
A
- Create a HashSet
- If set contains current value, return true
- If set doesn’t have current value, add to set
- If we never find duplicates, return false