All problems
Seqlock Read Retry
Problem
Count read retries in a seqlock with a pending writer.
operations contains "begin_write" (values[i] is the new value and the sequence becomes odd), "end_write" (publish the value and make the sequence even), or "read". A read that overlaps an in-progress write must retry once before observing the last committed value.
Return [last_read_value, retry_count].
Hint
An odd sequence number means a writer is mid-update, so the reader retries.
Test cases
case 1
in: [["begin_write","read","end_write","read"],[7,0,0,0]]
expected: [7,1]
case 2
in: [["begin_write","end_write","read"],[5,0,0]]
expected: [5,0]
case 3
in: [["read"],[0]]
expected: [null,0]
case 4
in: [["begin_write","read","read","end_write","read"],[1,0,0,0,0]]
expected: [1,2]
Starter codePython
def seqlock_read_retries(operations, values):
# Your code here
pass4 test cases · run your code against them in the editor