Count Known Commands That Prefix a Typed Shell Line

Last updated: November 29, 2026

Quick Overview

Given a list of command keywords and a typed line, return how many keywords are prefixes of the line, counting duplicates in the list.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given a list of command keywords and a typed line, return how many keywords are prefixes of the line, counting duplicates in the list.

A quick Google online assessment item framed around a shell or Cloud CLI autocomplete that checks which known commands the current line could be invoking. It checks a single walk down a trie that sums end counts, rather than calling startswith for each keyword.

What the Interviewer Expects
  • Insert keywords with end counts to handle duplicates
  • Walk the typed line once and sum end counts on the path
  • Stop at the first missing child
  • Contrast with O(n x L) startswith checks
  • Handle empty inputs
Key Topics to Cover
trie
prefix matching
counting
strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the longest matching command instead?
  • How would you handle tokenized commands like 'gcloud compute instances'?
  • What if the keyword list changes frequently?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: keywords, strings that may repeat, and line, a string. Output: the number of entries k in keywords (with multiplicity) such that line.startswith(k)....

High-Level Design

Every keyword that is a prefix of line lies on the single trie path spelled by line. So insert keywords with an end count per node, then walk line from the root and add the end count at each nod...


Submit Your Answer
Markdown supported