DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardMachine Coding

Machine Coding: High-Performance Search Autocomplete

16 views

Problem Statement

Implement a Search Autocomplete component (like Google Search) using vanilla JavaScript. The component should fetch suggestions from a mock API as the user types, but you must prevent unnecessary API calls and handle UI efficiency.

Requirements:

  • Debouncing: Implement a debounce mechanism to ensure the "API call" only triggers after the user has stopped typing for 300ms.
  • Caching: Store previous search results in memory so that if a user deletes text and types the same query again, the result is served instantly without an API call.
  • List Rendering: Display a list of results below the input. Clicking a result should fill the input field.
  • Keyboard Navigation: (Bonus) Allow users to navigate through results using the 'Up' and 'Down' arrow keys and select with 'Enter'.

Example:

If the user types "java", the mock API returns ['javascript', 'java tutorial', 'java compiler'].

Constraints:

  • Do not use any external libraries.
  • The API call should be simulated using setTimeout and a Promise.
  • Maximum cache size: 50 entries.

Starter Code:


class Autocomplete {
  constructor(element, options) {
    this.container = element;
    this.cache = new Map();
    this.init();
  }

  // Your implementation here
}

Sample Test Cases

Case 1
Input
["autocomplete-input", {"fetchSuggestions": "mockFetchSuggestions"}]
Expected Output
{"setup": true, "initialCacheSize": 0}
Case 2
Input
["autocomplete-input", {"fetchSuggestions": "mockFetchSuggestions", "actions": [{"type": "input", "value": "ja"}, {"type": "delay", "ms": 350}, {"type": "expect", "apiCalls": 1, "suggestions": ["javascript", "java", "python"]}]}]
Expected Output
{"apiCalls": 1, "suggestions": ["javascript", "java", "python"], "cacheHits": 0}
Case 3
Input
["autocomplete-input", {"fetchSuggestions": "mockFetchSuggestions", "actions": [{"type": "input", "value": "jav"}, {"type": "delay", "ms": 350}, {"type": "input", "value": "java"}, {"type": "delay", "ms": 350}, {"type": "input", "value": "jav"}, {"type": "delay", "ms": 350}, {"type": "expect", "apiCalls": 2, "suggestions": ["javascript", "java", "python"]}]}]
Expected Output
{"apiCalls": 2, "suggestions": ["javascript", "java", "python"], "cacheHits": 1}

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
16
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering