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
  1. Create an ans array with space twice the size of the input array
  2. loop through the the length of the input array
    2.1 And each time set
    result[i] = result[i+1] = input[i]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How could you return the indices of 2 elements that add up to the sum?

A
  1. Create a HashMap
  2. compl = target - current
  3. check if compl is in map, if it is return the indices
  4. if compl not in map, add it to map
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How could you check if a list contains duplicates?

A
  1. Create a HashSet
  2. If set contains current value, return true
  3. If set doesn’t have current value, add to set
  4. If we never find duplicates, return false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly