[Apple] Syncing Array Question
So i was given this question and still confused what this question is about:
you are given two arraies - A is before changes and another one B is after changes. Now we are told that those changes can be insertion, reordering or removal. There also an array C is currently equal to A and we need to update C so that C = B.
But, you can only use these 4 functions to modify C, and calls to those functions should be minimized:
for example:
given array A [2,4,5] and B [4, 5, 6],
you need to call
C.remove(at: 0) // to remove 2 from C, C = [4, 5]
C.insert(6) // add 6 at the end of C = [4, 5, 6]
after those calls, the array in class C is now in sync with array B.
example 2:
A: [1,2,3], B: [0, 3, 6]
should call:
C.remove(at: 0) // C = [2, 3]
C.remove(at:0) // C= [3]
C.insert(at:0, 1) // C = [0, 3]
C.insert(num: 6) // C = [0, 3, 6] = B, condition met, stop operation
example 3:
A: [], B [1,2,3]
should call:
C.insert(num: 1)
C.insert(num: 2)
C.insert(num: 3)
example 4:
A: [0, 3, 2], B [ 0, 2, 3]
should call:
C.remove(at: 1)
C.insert(num: 3)
limitations:
question:
1func syncData( arrA: [Int], arrB: [Int], arrC: C) {
2
3 \\todo
4
5 }
6
7
8 protocol C {
9 func remove( at: Int) throws
10 func insert(num: Int) throws
11 func insert(at: Int) throws
12 func get(at Int) throws
13}