← Grammarly Interview Insights

Grammarly·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Interviewed for a software engineer role at Grammarly and got a binary search question, which sounds easy until you're actually in the room second-guessing your index math.

Questions Asked (1)

Q1

Given a sorted integer array and a target value, return the index of the target if it exists, otherwise return -1. Then discuss variants like finding the leftmost or rightmost insertion point.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew binary search cold, or thought I did.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then present binary search as the optimal O(log n) solution. Walk through the standard search, then extend to variants like leftmost/rightmost insertion points, emphasizing how to adapt the binary search logic to handle duplicates and boundaries.

Pro tip: Mention that Python's bisect module provides built-in functions for these variants, but be prepared to implement them manually to demonstrate understanding. Also, discuss how these variants are used in real-world scenarios like maintaining sorted data or range queries.

1. Clarify requirements and edge cases

Ask if the array can contain duplicates, if it's sorted ascending, and what to return if not found. Consider empty array, single element, target smaller/larger than all elements.

2. Implement standard binary search

Use two pointers (low, high) and a while loop to find the target. Return index if found, else -1. Analyze time and space complexity.

3. Extend to leftmost insertion point

Modify binary search to find the first index where target can be inserted to keep array sorted (i.e., first element >= target). Handle duplicates by continuing search on the left when equal.

4. Extend to rightmost insertion point

Similarly, find the last index where target can be inserted (i.e., first element > target). Adjust condition to move right when equal.

5. Discuss trade-offs and applications

Compare linear vs binary search, mention built-in functions (e.g., bisect_left, bisect_right), and give examples where these variants are useful (e.g., range queries, maintaining sorted lists).

Key Points to Mention

  • Time complexity O(log n) and space complexity O(1) for iterative binary search.
  • Handling duplicates: leftmost and rightmost insertion points differ in equality condition.
  • Edge cases: empty array, target not present, all elements equal to target.
  • Use of mid = low + (high - low) // 2 to avoid overflow (in languages like Java/C++).
  • Python's bisect module: bisect_left and bisect_right for insertion points.
  • Real-world applications: maintaining sorted data, range queries, and database indexing.

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