← sunrise Interview Insights

sunrise·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Coding round at Sunrise for a software engineer role, heavy on classic algorithm and puzzle questions. Eight problems across linked lists, grids, sorting, and logic puzzles. No behavioral stuff at all, just back-to-back technical problems.

Questions Asked (8)

Q1

Reverse a singly linked list iteratively and/or recursively, returning the new head.

Algorithms & Data Structures
Author's notes

Classic warmup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: singly linked list, iterative and/or recursive, return new head. Then present the iterative solution with three pointers (prev, curr, next), walking through the pointer manipulation. If time permits, also present the recursive solution, explaining the base case and recursive step.

Pro tip: Always draw the linked list and pointer movements on a whiteboard or paper before coding; it prevents off-by-one errors and demonstrates systematic thinking. Also, mention edge cases like empty list or single node upfront.

1. Clarify requirements and edge cases

Confirm whether to implement iteratively, recursively, or both. Ask about input constraints (e.g., empty list, single node) and expected return (new head).

2. Explain the iterative approach

Describe using three pointers: prev (initially null), curr (head), and next. Iterate while curr is not null, reversing the link and advancing pointers.

3. Walk through an example

Trace the algorithm on a small list (e.g., 1->2->3) to show how pointers change and why it works. This builds confidence and catches errors.

4. Implement the code

Write clean, bug-free code for the iterative solution. If asked, also implement the recursive version, explaining the base case and recursive call.

5. Analyze complexity and test

State time and space complexity for both approaches. Discuss trade-offs (iterative uses O(1) space, recursive uses O(n) stack space). Suggest test cases.

Key Points to Mention

  • Iterative approach uses three pointers (prev, curr, next) and O(1) extra space.
  • Recursive approach uses call stack, O(n) space, but code can be more elegant.
  • Time complexity is O(n) for both, as each node is visited once.
  • Edge cases: empty list (return null), single node (return head).
  • Pointer manipulation order: save next, reverse link, advance prev and curr.
  • Return prev as the new head after the loop.

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

Q2

Given coordinates (x, y) on an infinite 2D grid filled by a counterclockwise outward square spiral starting at 1, compute the value at that cell in O(1) time without simulating the spiral.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one actually tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, identify the layer (ring) of the spiral that contains the given coordinate by computing the maximum of the absolute values of x and y. Then, determine the side of the square ring the point lies on and calculate the offset from the ring's starting corner to compute the exact value using arithmetic formulas.

Pro tip: Emphasize that the solution is O(1) by deriving a closed-form formula, and mention that you would validate it with a few test cases, including edge cases like the origin and points on the axes.

1. Determine the layer

Compute the layer number k as the maximum of |x| and |y|. This identifies which square ring the point belongs to.

2. Find the starting value of the layer

The largest value in layer k is (2k+1)^2, and the smallest is (2k-1)^2 + 1. Use these to establish the base value for the layer.

3. Identify the side and offset

Determine which side of the square the point lies on (right, top, left, or bottom) by checking the coordinates relative to the layer boundaries. Compute the offset along that side from the starting corner.

4. Compute the value

Use the base value and offset to calculate the final value with a simple arithmetic expression, ensuring the direction of the spiral (counterclockwise outward) is correctly accounted for.

5. Validate with edge cases

Test the formula with known points such as (0,0)=1, (1,0)=2, (1,1)=3, etc., to confirm correctness and handle any special cases.

Key Points to Mention

  • Layer identification using Chebyshev distance (max(|x|,|y|))
  • Closed-form arithmetic formulas for each side of the square
  • Constant time complexity O(1) and space complexity O(1)
  • Handling of edge cases such as origin, axes, and negative coordinates
  • Avoidance of simulation or iteration
  • Clear explanation of the spiral's counterclockwise outward pattern

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

Q3

100 bulbs are all off. Person k toggles every bulb that is a multiple of k. Which bulbs are on after all 100 people go?

Algorithms & Data Structures
Author's notes

Knew this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem by tracking how many times each bulb is toggled, which equals the number of divisors of its position. Recognize that only perfect squares have an odd number of divisors, so those bulbs remain on. Conclude that the bulbs at positions 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 are on.

Pro tip: Explain the reasoning clearly and connect it to the mathematical property of divisors; this shows you can derive a solution rather than just recall it. Also, mention that this is a classic problem often used to test pattern recognition and mathematical insight.

1. Understand the process

Clarify that each person k toggles bulbs at positions that are multiples of k, meaning bulb i is toggled once for each divisor of i.

2. Determine toggle count

For bulb i, the number of toggles equals the number of divisors of i. Initially off, so it ends on if toggled an odd number of times.

3. Identify odd divisor counts

Most numbers have an even number of divisors because they pair up, except perfect squares where one divisor is repeated (the square root).

4. Conclude which bulbs are on

Thus, only bulbs at perfect square positions (1, 4, 9, ..., 100) are toggled an odd number of times and remain on.

Key Points to Mention

  • Each bulb's final state depends on the parity of its number of toggles.
  • The number of toggles for bulb i equals the number of divisors of i.
  • Divisors typically come in pairs (d and i/d), leading to an even count.
  • Perfect squares have an odd number of divisors because the square root pairs with itself.
  • The bulbs that remain on are those at perfect square positions: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100.
  • This problem illustrates a common pattern in algorithm interviews: reducing a process to a mathematical property.

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

Q4

Given only a pointer to a node inside a singly linked list (no head pointer), delete that node from the list in O(1) time.

Algorithms & Data Structures
Author's notes

Copy the next node's value into the current node, then skip over the next node.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that since we don't have access to the previous node, we can't delete the given node directly. Instead, copy the data from the next node into the current node, then delete the next node. This achieves O(1) time complexity by effectively overwriting the current node with the next node's data and bypassing the next node.

Pro tip: Mention the edge case where the node to delete is the last node: in that case, this approach fails because there is no next node. In a real interview, discuss how you would handle it (e.g., mark it as dummy or assume it's not the last node).

1. Clarify constraints

Confirm that the node to delete is not the last node and that the list is singly linked. Also, ensure that the node is guaranteed to be in the list.

2. Explain the trick

Describe the approach: copy the data from the next node into the current node, then update the current node's next pointer to skip the next node.

3. Handle edge cases

Discuss what happens if the node is the last node: the approach doesn't work directly. Mention possible solutions or assumptions.

4. Analyze complexity

State that the time complexity is O(1) and space complexity is O(1), as we only modify pointers and copy data.

5. Provide code or pseudocode

Write the code: node.data = node.next.data; node.next = node.next.next; (and optionally free the next node in languages with manual memory management).

Key Points to Mention

  • The problem is a classic trick question: you can't delete the node itself without the head, but you can copy the next node's data and delete the next node.
  • Time complexity is O(1) because it involves only a few pointer manipulations and a data copy.
  • Space complexity is O(1) as no extra space is used.
  • Edge case: if the node is the last node, this method fails. In practice, you might mark it as a dummy or assume it's not the last node.
  • In languages with manual memory management (e.g., C/C++), you should free the next node to avoid memory leaks.
  • The list's structure remains intact except the given node's value is replaced and the next node is removed.

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

Q5

Four people with crossing times 1, 2, 5, and 6 minutes need to cross a bridge using one torch. At most two cross at a time and the pair moves at the slower person's pace. Find a schedule under 13 minutes.

Algorithms & Data Structures
Author's notes

The key insight is that you never want the slow people (5 and 6) to make a return trip.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by restating the problem and clarifying constraints. Then, explain the two main strategies for bridge crossing puzzles: the 'fastest two shuttle' method and the 'two slowest together' method. Finally, apply the optimal strategy step-by-step to achieve a total time under 13 minutes, verifying each move.

Pro tip: Demonstrate algorithmic thinking by comparing the two strategies and showing why the chosen one is optimal. This shows you can analyze trade-offs, which is crucial for software engineering.

1. Understand the problem

Restate the problem: four people with crossing times 1, 2, 5, 6 minutes, one torch, at most two cross at a time, pair moves at slower pace. Goal: total time < 13 minutes.

2. Identify strategies

Recognize two common strategies: (A) fastest two shuttle the torch to bring others over, (B) two slowest cross together while fastest return. Compare their total times.

3. Apply optimal strategy

Use strategy B: 1 and 2 cross (2 min), 1 returns (1 min), 5 and 6 cross (6 min), 2 returns (2 min), 1 and 2 cross (2 min). Total = 13 minutes. This is the known optimal solution for times 1, 2, 5, 6.

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

Q6

Given the root of a binary tree, determine whether it is a valid binary search tree.

Algorithms & Data Structures
Author's notes

Went with the min/max bounds approach rather than in-order traversal.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a recursive approach that passes down the allowable range (min, max) for each node, ensuring every node's value falls within its valid interval. Alternatively, perform an in-order traversal and verify that the sequence is strictly increasing. Clearly explain the chosen method, its time and space complexity, and handle edge cases like duplicate values.

Pro tip: Mention that a common mistake is only comparing a node with its immediate children; instead, emphasize the need to enforce global constraints via ranges or in-order traversal. Also, discuss how to handle duplicates based on the problem's definition (usually strict inequality).

1. Clarify the problem

Ask whether duplicate values are allowed and confirm the definition of a valid BST (e.g., left subtree < node < right subtree).

2. Choose an approach

Decide between the range-based recursive method or the in-order traversal method, and explain why one might be preferred.

3. Walk through the algorithm

Describe the steps of your chosen approach, including how you initialize and update the bounds or how you track the previous node during traversal.

4. Analyze complexity

State the time complexity (O(n)) and space complexity (O(h) for recursion stack or O(n) for iterative in-order with stack).

5. Handle edge cases

Discuss edge cases such as an empty tree, a single node, duplicate values, and skewed trees.

Key Points to Mention

  • Definition of a BST: all nodes in left subtree are less, all nodes in right subtree are greater.
  • Range-based recursion: each node must be within (min, max) bounds.
  • In-order traversal: yields sorted sequence if valid BST.
  • Time complexity O(n) and space complexity O(h) for recursion or O(n) for iterative.
  • Handling duplicates: typically not allowed, so use strict inequalities.
  • Common pitfall: only checking immediate children, which is insufficient.

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

Q7

Given a list of intervals, merge all overlapping ones and return the result.

Algorithms & Data Structures
Author's notes

Sort by start time, then sweep through merging whenever the current interval's start is within the previous one's end.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., whether intervals are sorted, inclusive/exclusive endpoints, and expected output format). Then propose a sort-based approach: sort intervals by start time, iterate through them, and merge overlapping intervals by comparing the current interval's start with the previous merged interval's end. Finally, analyze time and space complexity and discuss edge cases.

Pro tip: Mention that sorting is the key to achieving O(n log n) time, and that without sorting, the problem would require O(n^2) comparisons. Also, proactively discuss how you would handle edge cases like empty input or intervals that touch at endpoints.

1. Clarify requirements and constraints

Ask about input format, whether intervals are sorted, endpoint inclusivity, and expected output. Confirm if intervals are given as pairs [start, end] and if merging touching intervals (e.g., [1,2] and [2,3]) is required.

2. Choose an efficient algorithm

Propose sorting intervals by start time, then merging in a single pass. Explain why this yields O(n log n) time due to sorting, and O(n) space for the output.

3. Walk through the merge logic

Describe iterating through sorted intervals: if the current interval's start <= last merged interval's end, update the end to max of both ends; otherwise, add the last merged interval to the result and start a new one.

4. Analyze complexity and edge cases

State time and space complexity. Discuss edge cases: empty list, single interval, all overlapping, none overlapping, and intervals with same start times.

5. Test with examples

Walk through a concrete example, such as [[1,3],[2,6],[8,10],[15,18]] -> [[1,6],[8,10],[15,18]], to verify correctness and demonstrate understanding.

Key Points to Mention

  • Sorting intervals by start time is crucial for O(n log n) efficiency.
  • Merge condition: current.start <= lastMerged.end (or < if endpoints are exclusive).
  • Update the end of the merged interval to the maximum of the two ends.
  • Time complexity: O(n log n) due to sorting; space complexity: O(n) for the output.
  • Edge cases: empty input, single interval, intervals that touch at endpoints, and unsorted input.
  • Alternative approaches (e.g., using a stack) and why sorting is preferred.

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

Q8

Given an unsorted array, find the maximum difference between adjacent elements in the sorted version. Can you do better than O(n log n)?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The sub-O(n log n) approach uses a bucket/pigeonhole argument.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem and constraints, then propose a sorting-based O(n log n) solution as a baseline. Next, introduce the pigeonhole principle to achieve O(n) time by bucketing elements into n-1 intervals, and finally discuss trade-offs and edge cases.

Pro tip: Mention that the O(n) solution uses O(n) extra space, so if memory is constrained, sorting might be preferable. Also, handle duplicates and negative numbers gracefully.

1. Clarify the problem

Confirm that the array is unsorted, elements can be any integers (including negatives and duplicates), and we need the maximum difference between consecutive elements in the sorted order.

2. Baseline solution

Sort the array and scan adjacent elements to find the maximum difference. This takes O(n log n) time and O(1) extra space (if sorting in-place).

3. Optimized approach

Use the pigeonhole principle: if there are n elements, the maximum gap is at least ceil((max-min)/(n-1)). Create n-1 buckets of that size, distribute elements, and track min/max per bucket. The maximum gap is between the max of one bucket and the min of the next non-empty bucket.

4. Analyze complexity

The bucket approach runs in O(n) time and uses O(n) extra space. Compare with sorting: O(n log n) time, O(1) extra space. Discuss when each is preferable.

5. Handle edge cases

Consider arrays with fewer than 2 elements, all elements equal, or large ranges. Ensure the algorithm handles duplicates correctly (they fall into the same bucket, so gaps within buckets are zero).

Key Points to Mention

  • Pigeonhole principle: with n elements, the maximum gap is at least (max-min)/(n-1).
  • Bucket construction: use n-1 buckets of size (max-min)/(n-1), each storing min and max.
  • Maximum gap is found between max of one bucket and min of the next non-empty bucket.
  • Time complexity O(n) vs O(n log n) for sorting; space complexity O(n) vs O(1).
  • Edge cases: n < 2, all elements equal, negative numbers, duplicates.
  • Trade-offs: O(n) solution may have high constant factors and extra memory; sorting is simpler and often fast in practice.

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