software developer interview questions shared by candidates
you have array with n elements. How would you do circular shift of k positions? Time and space complexity?
Make a circular linklist, and move headpointer K position to do K shifts. It's O(n) time complexity. Space is contant. (circular link list).
Well, space isn't constant because you took an array and then copied it somehow to a linked list. Remember, you were given an array? If I understand the question correctly, they're asking to do a circular shift of some range of values, like the first k values in an array of length n? So if you wanted to shift right, temp = array[k] from index=k to 1 array[index] = array[index-1] array = temp this would be O(k)? I mean, it would take k steps, but maybe it's somehow still O(n)
oh, sorry, I misunderstood. Not k values, move everything k positions. Praveen Chettypally's answer works but the space complexity would be O(n) since there is a fully copy of the list? The simplest would probably be to make another array and copy in, starting at the (n-k)th element, going to the end, then starting at the beginning. A second array would probably be a better option than a completely different data structure. What if it has to be done in place? is there an O(n) solution?
See Interview Questions for Similar Jobs
- Senior Software Engineer
- Software Developer
- Software Development Engineer
- Business Analyst
- Technical Support Engineer
- Product Manager
- Principal Software Engineer
- QA Engineer
- Senior Software Developer