Interviewer: Left shift by 4 in-place, without slicing
data=[1,2,3,4,5,6,7,8,9]shifts=4shifts=shifts%(len(data))# Reverse entire array
foriinrange(len(data)//2):tmp=data[i]data[i]=data[-i-1]data[-i-1]=tmpshifts=len(data)-shifts# Reverse first k items
foriinrange(shifts//2):tmp=data[i]data[i]=data[shifts-i-1]data[shifts-i-1]=tmp# Reverse the rest
foriinrange((len(data)-shifts)//2):tmp=data[shifts+i]data[shifts+i]=data[-i-1]data[-i-1]=tmpprint(data)
Interviewer: Right shift by 5 in-place, without slicing
data=[1,2,3,4,5,6,7,8,9]shifts=5shifts=shifts%(len(data))# Reverse entire array
foriinrange(len(data)//2):tmp=data[i]data[i]=data[-i-1]data[-i-1]=tmp# Reverse first k items
foriinrange(shifts//2):tmp=data[i]data[i]=data[shifts-i-1]data[shifts-i-1]=tmp# Reverse the rest
foriinrange((len(data)-shifts)//2):tmp=data[shifts+i]data[shifts+i]=data[-i-1]data[-i-1]=tmpprint(data)