Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Create a React component that counts how many times a specific key (e.g., 'a') is pressed on the keyboard.

Easy

Problem Statement

Create a React component that counts how many times a specific key (e.g., 'a') is pressed on the keyboard.

Sample Output

Key 'a' pressed: 0 (increases each time 'a' is pressed).

Limits

Time: 2s · Memory: 256 MB

Code Editor

Try your code in this preview. Submit marks it done.

import React, { useState, useEffect } from "react";
 
 const KeyPressCounter = () => {
  const [count, setCount] = useState(0);
 
  useEffect(() => {
  const handleKeyPress = (event) => {
  // Increment the count if "a" is pressed
  };
 
  // Add event listener
  return () => {
  // Cleanup event listener
  };
  }, []);
 
  return <p>Key "a" pressed: {count}</p>;
 };
 
 export default KeyPressCounter;