Shuffle String (Python Interview)

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

Want to give it a shot?? Sign up with us to make sure your upcoming interviews are the best they can be by practicing with our experienced engineers. @t

interviewingio
Автор

Ugh, the uncomfortable jokes from the interviewee were so painful to hear, poor guy.

MichaelMerritt
Автор

Well honestly I think everyone here just forgot what the interviewer said "Equally likely" and just went with what the Interviewee interpreted it as "Random".
Many approaches here, not all, but including some attempts by the interviewee don't make the end result have an equal probability for every character basically the only requirement by the interviewer. He did say it didn't have to be exactly correct, but when proposing a "better" solution consider that you should've asked how big is the string allowed to be and how efficient it needs to be compared to how maintainable it should be

joachimolsson
Автор

When people use python and still do C style swapping, it feels awkward. Just use
a, b= b, a style which is more pythonic

amanpreetwaliacool
Автор

My way of doing it. The length will always match, and it'll say if a character isn't being shuffled. rand.shuffle() seems like cheating, and then you would not be able to see if the string is actually getting shuffled.


def shuffle_string(text):
positions = list(range(len(text)))
new_string = [""] * len(positions)

for i in range(len(text)):
pos = random.randint(0, len(positions) - 1)

if (i == positions[pos]):
print(f'Not shuffling character: {text[i]}')
print(f'{text}\n{" " * i}^\n')

= text[i]

return ''.join(new_string)

print(shuffle_string("Hello World"))

benstigsendev
Автор

Do they know that they're being recorded and will be open to the public before hand? That would make things a bit more nervous.

cisdolce
Автор

Hey, I think this is a very simple approach but logically speaking this is really just a permutation problem with a set of n characters available and a condition that no character is repeated again at the same position.

adriansilva
Автор

He could have used randint and used the integer as an index

xhir
Автор

Why would you talk to yourself and make assumptions instead of asking questions and then talk to yourself so that interviewer can understand what's the approach

AnuragDeshpande
Автор

It appears there is an inbuilt "shuffle" function in "random" module. Here is my code without using that.

CODE
import random

def shuffle_str(input_str):
char_list = list(input_str)
output_str = ''

for ctr in range(0, len(input_str)):
random_pos = random.randrange(0, len(char_list))
output_str = output_str + char_list[random_pos]
del char_list[random_pos]

return output_str

input_str = 'georgebush'
print ()
print ("Input String:", input_str)
print ("Shuffled String:", shuffle_str(input_str))

input_str = 'barackobama'
print ()
print ("Input String:", input_str)
print ("Shuffled String:", shuffle_str(input_str))

OUTPUT
Input String: georgebush
Shuffled String: ugeogrsheb

Input String: barackobama
Shuffled String: mcoaakabbar

vallamsenthil
Автор

Interviewer is wrong that you shouldn't care about the prior state. For example, someone could write a coin flip algorithm that goes flip(coin) { return !coin; } which has a 50/50 distribution but isn't random.

CarrotCakeMake
Автор

My goodness that was painful.. poor guy must have felt awful. I have more appreciation for interviewers now because it's not easy to keep a straight face after so many direct hints over and over.

BGivo
visit shbcf.ru