Most Asked Coding Interview Question (Don't Skip !!😮) #shorts

preview_player
Показать описание
Most Asked Coding Interview Question (Don't Skip !!😮) #shorts

Do you want to excel in your coding interview?🤔
Stay connected with Programiz as we are bringing the most asked coding interview questions for our subscribers. 😲👏

If you are a Python beginner, you can learn more from our video tutorials:

Here the question is,
"From the list of numbers, move zero to the end of the list.
list = [1, 0, 2, 0, 4, 6]."

✍️ Comment down below if you can solve this question in another language other than python. Also, can you solve this question in a much simpler way? Let us know by commenting down below.

For more amazing content, please consider subscribing. And, do turn on your notifications so that you will be notified whenever we post our videos.

PS. Find us elsewhere in

#shorts #programiz #coding #interview #codinginterview #question #python
Рекомендации по теме
Комментарии
Автор

this solution is o(n2).

just create a new array, copy the items if it is not 0 and add 0 at the last until the length is equal. that's o(n) with the same order.

lequdindoquede
Автор

1) Don't call a variable 'list' which is a built-in keyword. It doesn't met the best practice.
2) Check the complexity of your methods: remove is in O(n) but you could use del which is not. During an interview, always ask for what case you should improve (best case: no '0', mean case: ~half of '0' or worst case: only '0'). Indeed, by rewriting all the code, you can improve the computational time with a factor greater than 4000 in the mean case and 6000 in the worst case:

n = len(items)
items.__init__([item for item in items if item != 0])

misterkite
Автор

That's a terrible solution. You can keep exchanging 0 with the last index using 2 pointer technique.

AsifIqbalR
Автор

for efficiency and memory:

def move_zeros(nums):
insert_pos = 0
for num in nums:
if num != 0:
nums[insert_pos] = num
insert_pos += 1


while insert_pos < len(nums):
nums[insert_pos] = 0
insert_pos += 1

oommggdude
Автор

for i in range(len(list)):
if list[i] == 0:
list.insert(len(list), list.pop(i))

oommggdude
Автор

You would assume this question would only be asked in a language where array sizes are static…

mrichards
Автор

Just a suggestion if you added commentary explaining, instead of music I think that’d increase viewership 👍

alhaqqislam
Автор

Nice exercise, thanks; but, removing and appending elements in a list while iterating on it is very dangerous, this particular example works, but be careful, in a real world situation you may get in trouble

diegoc.
Автор

Now do it in linear time and in-place. That's what your interviewer will probably ask of you from this question.

bufdud
Автор

Most optimal solution:
i=0, j=0
while j<len(list):
if list[j]!=0:
list[i], list[j] = list[j], list[i]
i+=1
j+=1
else:
j+=1

InsightInAptitude
Автор

ListB = [ x for x in list if x != 0 ] + [0] * list.count(0)

NilanjalChoudhary-wpgw
Автор

This is a poor solution because each remove method takes linear time.

A better solution would be to create a separate array and count variable, and for each non-zero number append it to the separate array, and for each zero update the counter variable.

Then, you can append zero to the separate array count times, which results in a one-pass stable linear solution.

juicy
Автор

This is with two pointers as suggested, do note that the order of elements is destroyed. This runs in O(n) with constant space complexity (in-place operation):

# Set pointers
left = 0
right = len(items) - 1
while items[right] == 0 and left < right:
right -= 1

while left < right:
# swap
if items[left] == 0:
swp = items[left]
items[left] = items[right]
items[right] = swp

# Adjust pointers.
left += 1
while items[right] == 0 and left < right:
right -= 1

willemvanderspek
Автор

def move_zeroes(nums):
pos = 0 # position to place the next non-zero number

for i in range(len(nums)):
if nums[i] != 0:
nums[i], nums[pos] = nums[pos], nums[i]
pos += 1

return nums

dmitricherleto
Автор

You should try removing items while iterating them in java

eeeeeeee
Автор

#item = [1, 0, 2, 0, 4, 5]
#for i in range(len(item)):
# for j in range(i, len(item)):
# if item[i]==0:
# k=item[i]
# item[i]=item[j]
# item[j]=k
#print (item)

ankitsoni
Автор

Might not be best in terms of memory but using a new array to store elements either at tail or head could result in faster output as it will not rerun operation at zeroes in end and also it will skip delete computation .

muhammadibrahim
Автор

If you do this in your interview you probably gonna be fired 😂

Jack-ssre
Автор

I guess we can take a variable count which will count the number of zeros
First we take a new list and copy each non zero element of the given list to the new list and if it zero we increament count .
Finally we append the count number of zeros at the end of the new list.

Cubeone
Автор

Half the solutions I see to these problems are terrible. It seems any beginner can just record themselves solve an easy problem inefficiently, get a million views and be heralded as a coding genius

darthramen