← Weride Interview Insights

Weride·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026Remote

Summary

Third round at Weride with someone based in China. Split pretty evenly between talking through my resume and doing coding problems, which I wasn't fully expecting going in.

Questions Asked (2)

Q1

Implement a function that generates a random integer between 1 and 10 using only a given function that generates a random integer between 1 and 7.

Algorithms & Data Structures
Author's notes

Classic rejection sampling problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use rejection sampling to map the 1-7 random generator to a uniform distribution over 1-10. Generate a number in a larger range (e.g., 1-49) by combining two calls, then reject values outside a multiple of 10 and use modulo to get 1-10. Ensure the method is unbiased and discuss efficiency.

Pro tip: Mention that rejection sampling is unbiased and that the expected number of calls is constant (about 2.45), showing you understand both correctness and performance. Also, note that simpler methods like summing two rand7 calls introduce bias, which is a common pitfall.

1. Understand the problem and constraints

Clarify that rand7() returns each integer from 1 to 7 with equal probability, and we need a uniform distribution over 1 to 10. Emphasize that the solution must be unbiased and use only rand7().

2. Design an unbiased mapping

Combine two calls to rand7() to create a uniform distribution over a larger range, such as 1-49, by computing (rand7()-1)*7 + rand7(). This gives each number from 1 to 49 with equal probability.

3. Apply rejection sampling

Reject outcomes greater than 40 (the largest multiple of 10 within 1-49) and map the remaining 1-40 uniformly to 1-10 using modulo: ((value-1) % 10) + 1. This ensures each number 1-10 has probability 4/49 per trial.

4. Analyze efficiency and correctness

Calculate the expected number of rand7() calls: each trial uses 2 calls and succeeds with probability 40/49, so expected calls = 2 * (49/40) = 2.45. Confirm the distribution is uniform and discuss potential optimizations or alternative methods.

5. Implement and test

Write clean code with a loop that repeats until a valid number is obtained. Test with a large number of samples to verify uniformity, and handle edge cases like infinite loops (though probability is zero).

Key Points to Mention

  • Rejection sampling ensures an unbiased uniform distribution.
  • Combining two rand7() calls yields a uniform distribution over 1-49.
  • Rejecting values >40 and using modulo 10 gives uniform 1-10.
  • Expected number of rand7() calls is 2.45, showing efficiency.
  • Alternative methods like summing two rand7() calls are biased and incorrect.
  • The solution is generalizable to any range by adjusting the rejection threshold.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Given a stream of points added to a 2D plane, implement a data structure that can efficiently count the number of axis-aligned squares that can be formed using a queried point as one of the corners.

Algorithms & Data StructuresData Modeling
Author's notes

This one tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., number of points, query frequency, coordinate range) and discuss trade-offs between preprocessing and query time. Propose a hash-based solution: store points in a hash set for O(1) lookups, and for each query point, iterate over all possible side lengths determined by existing points sharing the same x or y coordinate, checking the other two corners in the set. Analyze time complexity: O(n) per query in the worst case, but can be optimized by grouping points by x and y coordinates.

Pro tip: Mention that you can preprocess points into hash maps keyed by x and y coordinates to quickly find candidate side lengths, and discuss how to handle duplicate points or collinear points that don't form squares. Also, consider if the query point can be any corner (not just bottom-left) and adjust the algorithm accordingly.

1. Clarify requirements and constraints

Ask about the number of points, query frequency, coordinate ranges, and whether points can be duplicated. Confirm that the square must be axis-aligned and the queried point is one of the four corners.

2. Choose data structures

Use a hash set to store all points for O(1) membership checks. Additionally, maintain hash maps grouping points by x-coordinate and by y-coordinate to efficiently find candidate side lengths.

3. Design query algorithm

For a query point (x, y), iterate over all points with the same x-coordinate (or y-coordinate) to determine possible side lengths. For each candidate side length d, check if the other two corners (x±d, y) and (x±d, y±d) (or appropriate combinations) exist in the set.

4. Handle edge cases and optimize

Consider duplicate points, points that form degenerate squares (side length 0), and ensure the algorithm counts each square only once. Optimize by choosing the smaller of the two groups (same x or same y) to iterate over.

5. Analyze complexity and discuss trade-offs

State time complexity: O(min(k_x, k_y)) per query, where k_x and k_y are the number of points sharing the query's x or y coordinate. Space complexity O(n). Discuss potential improvements like spatial indexing if needed.

Key Points to Mention

  • Hash set for O(1) point existence checks
  • Grouping points by x and y coordinates to find candidate side lengths
  • Iterating over the smaller group to reduce query time
  • Handling all four possible orientations of the square relative to the query point
  • Avoiding double-counting and handling duplicate points
  • Time and space complexity analysis with trade-offs

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.