Read and Critique a Short Python Function That Randomly Swaps List Elements

Read the full interview experience this question came from →

Quick Overview

A code-reading exercise: assess a short Python function that walks a list and swaps each element with a randomly chosen position at or before it. Tests tracing unfamiliar code, reasoning about whether every ordering is equally likely, input-type edge cases such as strings, and practical code-review judgment.

Read and Critique a Short Python Function That Randomly Swaps List Elements

Company: EliseAI

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Technical Screen

At the end of a 30-minute software engineer phone screen, the interviewer shares a short snippet and asks what you think of it. It is reproduced here in Python: ```python def foo(a): for i in range(len(a)): j = math.floor(random.random() * (i + 1)) temp = a[i] a[i] = a[j] a[j] = temp return a ``` Assume `math` and `random` are the Python standard-library modules. Read the function and give your assessment as you would in a code review: what it does, whether it does that correctly, and what you would change. ```hint Trace a tiny input Run the loop by hand on a three-element list and note which values `j` can take at each step. ``` ```hint Count the outcomes Count how many equally likely sequences of `j` values the loop can produce, and compare that with the number of possible orderings of the input. ``` ### Clarifying Questions - What will callers pass as `a`: always a list, or sometimes a string or another kind of sequence? - Do callers expect their own list to change, or a new object back? - What is the result used for? Is it enough that every outcome is equally likely, or must outcomes also be impossible to predict? ### What a Strong Answer Covers - Identifying what the function does from a trace rather than a guess - Whether every possible ordering is equally likely, with a justification rather than an assertion - Behavior on inputs a caller might pass: empty and single-element lists, strings, shared references - Code-review judgment on the function's contract and readability, with concrete, justified changes - The randomness source: testability, predictability and the generator's limits ### Follow-up Questions - Suppose `j` were drawn from the whole list on every iteration instead of from `0` to `i`. Would every ordering still be equally likely? Why? - How would you test, in code, that this function is unbiased? - How would you use it to reorder the characters of a string? - Would you use it to shuffle the deck in an online card game where players must not be able to predict the order? What would you change?

Overview: A code-reading exercise: assess a short Python function that walks a list and swaps each element with a randomly chosen position at or before it. Tests tracing unfamiliar code, reasoning about whether every ordering is equally likely, input-type edge cases such as strings, and practical code-review judgment.

Read the full EliseAI Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/EliseAI
EliseAI logo
EliseAI
Jul 5, 2026
easySoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

At the end of a 30-minute software engineer phone screen, the interviewer shares a short snippet and asks what you think of it. It is reproduced here in Python:

def foo(a):
    for i in range(len(a)):
        j = math.floor(random.random() * (i + 1))
        temp = a[i]
        a[i] = a[j]
        a[j] = temp
    return a

Assume math and random are the Python standard-library modules. Read the function and give your assessment as you would in a code review: what it does, whether it does that correctly, and what you would change.

Clarifying Questions Guidance

  • What will callers pass as a : always a list, or sometimes a string or another kind of sequence?
  • Do callers expect their own list to change, or a new object back?
  • What is the result used for? Is it enough that every outcome is equally likely, or must outcomes also be impossible to predict?

What a Strong Answer Covers Guidance

  • Identifying what the function does from a trace rather than a guess
  • Whether every possible ordering is equally likely, with a justification rather than an assertion
  • Behavior on inputs a caller might pass: empty and single-element lists, strings, shared references
  • Code-review judgment on the function's contract and readability, with concrete, justified changes
  • The randomness source: testability, predictability and the generator's limits

Follow-up Questions Guidance

  • Suppose j were drawn from the whole list on every iteration instead of from 0 to i . Would every ordering still be equally likely? Why?
  • How would you test, in code, that this function is unbiased?
  • How would you use it to reorder the characters of a string?
  • Would you use it to shuffle the deck in an online card game where players must not be able to predict the order? What would you change?
Loading comments...