Coding Interview: Reverse Array in Place - Whiteboard Thursday

  Рет қаралды 19,600

Irfan Baqui

Irfan Baqui

6 жыл бұрын

Check out the detailed data structures and algorithms course at www.interviewaccelerator.com
Here is the problem I'll cover in next week's Whiteboard Thursday:
You're given an array of integers where each element contains the index to the next element. Determine if the array has a cycle.
Try it out yourself and leave your answers in the comments below. And subscribe to get notified of the solution next week.
For advanced data structures and algorithms practice, visit irfanbaqui.com/coding-intervi...

Пікірлер: 27
@rahul.gupta.96
@rahul.gupta.96 6 жыл бұрын
It's always nice to see coding problems in an interview environment. Thanks for taking out the time for doing this. It's really helpful and mostly, it's fun to watch. 😄 Eagerly waiting for the next week. 😋
@IrfanBaqui
@IrfanBaqui 6 жыл бұрын
Thanks for the kind words, Rahul!
@jasonwelsh417
@jasonwelsh417 6 жыл бұрын
This Whiteboard Thursday series is amazing. Thank you so much!
@IrfanBaqui
@IrfanBaqui 6 жыл бұрын
I'm so glad you like it :)
@PadillaJosh
@PadillaJosh 5 жыл бұрын
Awesome vids man. Been watching your vids in prep for my interviews.
@saurabhmishra8833
@saurabhmishra8833 6 жыл бұрын
I like the series ,actually the first series of tutorials that I found useful and interesting .Please make more videos
@travhenry3451
@travhenry3451 3 жыл бұрын
This was awesome! Keep up the great work!
@ridhwaans
@ridhwaans 5 жыл бұрын
this week's question was an easy one
@anirban_j
@anirban_j 5 жыл бұрын
@Irfan Don't you think time complexity should be n/2 becoz you have condition while (front
@Davidh0330
@Davidh0330 5 жыл бұрын
The complexity is still linear time so it's o(n)
@APMathNerd
@APMathNerd 5 жыл бұрын
O(n/2) is the same as O(n). We ignore constant factors and added lower order terms when talking about time complexity (e.g., O(3n+7) is also the same as O(n)).
@shrajan28
@shrajan28 5 жыл бұрын
can you explain time complexity, how its linear?
@tannerbarcelos6880
@tannerbarcelos6880 3 жыл бұрын
Loop through the array , swaps are constant. O(n/2) sure, but you actually loop the whole array using two pointers that compress, so, it’s O(n) where n is the number of elements traversed in this array
@JossinJax
@JossinJax 5 жыл бұрын
Why is the back index array.length -1? What if the value of index zero, zero?
@iliketocode6986
@iliketocode6986 3 жыл бұрын
i saw this in a book and it hurt my brain.
@pratapdafedar7305
@pratapdafedar7305 6 жыл бұрын
//Tip#2: Swapping 2 elements in an array without using any temp or extra space. function Swap(Arr, front, back) { Arr[front] = Arr[front] + Arr[back]; Arr[back] = Arr[front] - Arr[back]; Arr[front] = Arr[front] - Arr[back]; }
@nands4410
@nands4410 6 жыл бұрын
Pratap dafedar Using ^ would be better than this
@jackxlifer6346
@jackxlifer6346 5 жыл бұрын
Sum operation could overflow, or we can loose precision for float numbers, so temp variable is better option.
@alxjones
@alxjones 5 жыл бұрын
Both add swap and xor swap are strictly worse than temp swap in 99% of applications. Compilers will optimize away the temp variable, which means that temp swap won't use any additional space, but will rather use a built in swap op in the processor instruction set. This is the most efficient way to do it. The add swap and xor swap both are slower because they often won't get optimized away, and each operation requires the results from the last meaning that parallelization is not possible. Also, add swap can have issues with integer overflow, float overflow and precision, while xor swap can create problems with aliasing. Lastly, neither of these two swaps will work for objects in java, since there are no pointers to even use as a work-around, not to mention that they are very unintuitive to people reading that code that aren't familiar with those techniques. In the end, use temp swap. But it doesn't hurt to bring up other methods of swapping (or doing anything) in an interview if you can explain when they are better and when they are worse.
@rmadhavmca1
@rmadhavmca1 5 жыл бұрын
I like your videos, Could you please also explain time complexity of each of your video. thanks in advance
@misslondonstani
@misslondonstani 3 жыл бұрын
Can’t you just use a stack?
@scuba485
@scuba485 6 жыл бұрын
Kindly Request the lady interviewer to increase her volume..many videos her voice is not audible
@adrianharo6586
@adrianharo6586 5 жыл бұрын
Perv
@hand__banana
@hand__banana 4 жыл бұрын
@@adrianharo6586 are you dense?
@chetanpalekar9366
@chetanpalekar9366 5 жыл бұрын
this problem can be implemented with n/2 time complexity. github.com/ChetanPalekar/JAVA-/blob/master/arrayInverseInPlace.java
@Christopherney
@Christopherney 5 жыл бұрын
/** * Reverse an array in place. Time complexity O(n/2), Space complexity O(1) * @param arr Array to reverse * @return Reverse array */ public static int[] reverseArrayInPlace(int[] arr) { int mid = arr.length / 2; for (int leftIndex = 0; leftIndex < mid; leftIndex++) { int rightIndex = arr.length - leftIndex - 1; int leftValue = arr[leftIndex]; int rightValue = arr[rightIndex]; arr[leftIndex] = rightValue; arr[rightIndex] = leftValue; } return arr; }
@george.5c4
@george.5c4 4 жыл бұрын
A bit slower implementation but ok.
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 28 МЛН
Can You Draw A PERFECTLY Dotted Line?
00:55
Stokes Twins
Рет қаралды 79 МЛН
Find the intersection between arrays: Coding Interview Question
11:26
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 261 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 151 М.
Amazon Coding Interview Question - Recursive Staircase Problem
20:01
Facebook Interview: K Most Frequent Elements - Whiteboard Thursday
14:24
Technical Interview At MIU
2:19
Dave Girma
Рет қаралды 13 М.
Main filter..
0:15
CikoYt
Рет қаралды 13 МЛН
Игровой Комп с Авито за 4500р
1:00
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 1,6 МЛН
Low Price Best 👌 China Mobile 📱
0:42
Tech Official
Рет қаралды 717 М.