Coding Questions
Coding Questions
Developers
Grok 3
July 2025
Contents
1 Introduction 3
3 JavaScript Fundamentals 3
3.1 Question 1: Reverse a String . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Question 2: Find the Longest Word . . . . . . . . . . . . . . . . . . . . . 4
3.3 Question 3: Check if a String is a Palindrome . . . . . . . . . . . . . . . 4
3.4 Question 4: Remove Duplicates from an Array . . . . . . . . . . . . . . . 4
3.5 Question 5: Check if Two Strings are Anagrams . . . . . . . . . . . . . . 4
3.6 Question 6: Count Vowels in a String . . . . . . . . . . . . . . . . . . . . 4
3.7 Question 7: Find the Largest Number in an Array . . . . . . . . . . . . . 5
3.8 Question 8: Check if a Number is Prime . . . . . . . . . . . . . . . . . . 5
3.9 Question 9: Calculate Factorial of a Number . . . . . . . . . . . . . . . . 5
3.10 Question 10: Remove Whitespace from a String . . . . . . . . . . . . . . 5
3.11 Question 11: Capitalize First Letter of Each Word . . . . . . . . . . . . . 6
3.12 Question 12: Check if String Contains Only Digits . . . . . . . . . . . . 6
3.13 Question 13: Sum Numbers in an Array . . . . . . . . . . . . . . . . . . 6
3.14 Question 14: Convert String to camelCase . . . . . . . . . . . . . . . . . 6
3.15 Question 15: Find Array Intersection . . . . . . . . . . . . . . . . . . . . 7
1
4.12 Question 27: Add Item to Cart . . . . . . . . . . . . . . . . . . . . . . . 11
4.13 Question 28: Calculate Cart Total . . . . . . . . . . . . . . . . . . . . . . 11
4.14 Question 29: Remove Item from Cart . . . . . . . . . . . . . . . . . . . . 12
4.15 Question 30: Create Cart Index . . . . . . . . . . . . . . . . . . . . . . . 12
5 React 12
5.1 Question 31: Counter Component . . . . . . . . . . . . . . . . . . . . . . 12
5.2 Question 32: Fetch and Display Data . . . . . . . . . . . . . . . . . . . . 13
5.3 Question 33: Controlled Input Component . . . . . . . . . . . . . . . . . 14
5.4 Question 34: Pass Props to Child Component . . . . . . . . . . . . . . . 14
5.5 Question 35: Auto-Focus Input . . . . . . . . . . . . . . . . . . . . . . . 14
5.6 Question 36: Toggle Button Component . . . . . . . . . . . . . . . . . . 15
5.7 Question 37: Theme Switching with Context API . . . . . . . . . . . . . 15
5.8 Question 38: Form Submission . . . . . . . . . . . . . . . . . . . . . . . 16
5.9 Question 39: Dynamic List Rendering . . . . . . . . . . . . . . . . . . . 17
5.10 Question 40: Update Document Title . . . . . . . . . . . . . . . . . . . . 17
5.11 Question 41: Toggle Visibility . . . . . . . . . . . . . . . . . . . . . . . . 18
5.12 Question 42: Deletable List . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.13 Question 43: Timer Component . . . . . . . . . . . . . . . . . . . . . . . 19
5.14 Question 44: Multiple Input Form . . . . . . . . . . . . . . . . . . . . . . 19
5.15 Question 45: Conditional Rendering . . . . . . . . . . . . . . . . . . . . 20
6 Problem-Solving 20
6.1 Question 46: Second Largest Number . . . . . . . . . . . . . . . . . . . . 20
6.2 Question 47: Balanced Parentheses . . . . . . . . . . . . . . . . . . . . . 20
6.3 Question 48: Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . . . 21
6.4 Question 49: Create Product Route . . . . . . . . . . . . . . . . . . . . . 21
6.5 Question 50: Reverse Words in a Sentence . . . . . . . . . . . . . . . . . 22
6.6 Question 51: First Non-Repeating Character . . . . . . . . . . . . . . . . 22
6.7 Question 52: Filter List Component . . . . . . . . . . . . . . . . . . . . . 22
6.8 Question 53: Merge Sorted Arrays . . . . . . . . . . . . . . . . . . . . . 23
6.9 Question 54: Request Logger Middleware . . . . . . . . . . . . . . . . . . 23
6.10 Question 55: Power of Two . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.11 Question 56: Rotate Array . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6.12 Question 57: Longest Common Prefix . . . . . . . . . . . . . . . . . . . . 24
6.13 Question 58: Valid Email . . . . . . . . . . . . . . . . . . . . . . . . . . 24
6.14 Question 59: Products by Price Range . . . . . . . . . . . . . . . . . . . 24
6.15 Question 60: Two Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2
1 Introduction
This document contains 60 questions and answers for a MERN stack technical test,
designed for candidates with approximately one year of experience. The questions are
divided into four categories: JavaScript Fundamentals, MongoDB Schema Design, React,
and Problem-Solving. Each category includes 15 questions, covering practical coding,
theoretical concepts, and problem-solving skills relevant to the MERN stack (MongoDB,
[Link], React, [Link]). A strategy for approaching the test is also provided.
3 JavaScript Fundamentals
3.1 Question 1: Reverse a String
Write a function to reverse a string.
1 function reverseString (str) {
2 if (! str) return "";
3 return str. split (''). reverse ().join ('');
4 }
5 console .log( reverseString (" hello ")); // Output : " olleh "
6 console .log( reverseString ("")); // Output : ""
3
3.2 Question 2: Find the Longest Word
Write a function to find the longest word in a sentence.
1 function longestWord ( sentence ) {
2 if (! sentence ) return "";
3 return sentence . split (' '). reduce (( longest , current ) =>
4 current . length > longest . length ? current : longest , '');
5 }
6 console .log( longestWord ("I love coding very much ")); // Output : "
coding "
4
1 function countVowels (str) {
2 const vowels = ['a', 'e', 'i', 'o', 'u '];
3 return str. toLowerCase (). split (''). filter (char => vowels .
includes (char)). length ;
4 }
5 console .log( countVowels (" hello world ")); // Output : 3
5
3.11 Question 11: Capitalize First Letter of Each Word
Write a function to capitalize the first letter of each word in a string.
1 function capitalizeWords (str) {
2 if (! str) return "";
3 return str. split (' ').map(word =>
4 word. charAt (0). toUpperCase () + word. slice (1). toLowerCase ()
5 ).join(' ');
6 }
7 console .log( capitalizeWords (" hello world ")); // Output : " Hello World
"
6
3.15 Question 15: Find Array Intersection
Write a function to find the intersection of two arrays.
1 function arrayIntersection (arr1 , arr2) {
2 const set = new Set(arr2);
3 return [... new Set(arr1. filter (item => [Link](item)))];
4 }
5 console .log( arrayIntersection ([1 , 2, 3, 4], [2, 4, 6])); // Output :
[2, 4]
7
5 try {
6 const posts = await [Link] ({ author : userId }). populate ('
author ', 'username ');
7 return posts;
8 } catch ( error ) {
9 console .error (' Error fetching posts :', error );
10 throw error;
11 }
12 }
8
9 { new: true }
10 );
11 return post;
12 } catch ( error ) {
13 console .error (' Error updating post:', error );
14 throw error;
15 }
16 }
9
4.8 Question 23: Delete a Post
Write a function to delete a post.
1 const mongoose = require ('mongoose ');
2 const Post = require ( './ models /Post ');
3
10
1 const mongoose = require ('mongoose ');
2
11
8 { $unwind : '$items ' },
9 { $lookup : { from: 'products ', localField : 'items .
product ', foreignField : '_id ', as: 'productData ' } },
10 { $unwind : '$productData ' },
11 { $group : {
12 _id: '$_id ',
13 total : { $sum: { $multiply : [' $items .quantity ', '
$productData .price '] } }
14 }}
15 ]);
16 return result [0]?. total || 0;
17 } catch ( error ) {
18 console .error (' Error calculating total :', error );
19 throw error;
20 }
21 }
5 React
5.1 Question 31: Counter Component
Create a counter component using useState.
12
1 import React , { useState } from 'react ';
2
3 function Counter () {
4 const [count , setCount ] = useState (0);
5
6 return (
7 <div >
8 <p>Count : { count }</p>
9 <button onClick ={() => setCount ( count + 1)}>Increment </
button >
10 <button onClick ={() => setCount ( count - 1)}>Decrement </
button >
11 </div >
12 );
13 }
14
3 function DataFetcher () {
4 const [data , setData ] = useState ([]);
5 const [loading , setLoading ] = useState (true);
6
20 return (
21 <div >
22 { loading ? <p> Loading ... </p> : (
23 <ul >
24 {[Link](post => (
25 <li key ={ [Link]}>{ post. title }</li >
26 ))}
27 </ul >
28 )}
13
29 </div >
30 );
31 }
32
3 function TextInput () {
4 const [text , setText ] = useState ('');
5
6 return (
7 <div >
8 <input
9 type =" text"
10 value ={ text}
11 onChange ={(e) => setText (e. target . value )}
12 placeholder =" Type something ..."
13 />
14 <p>You typed : {text }</p>
15 </div >
16 );
17 }
18
7 function Parent () {
8 return <Child message =" Hello from Parent !" />;
9 }
10
14
1 import React , { useRef , useEffect } from 'react ';
2
3 function AutoFocusInput () {
4 const inputRef = useRef (null);
5
10 return <input ref ={ inputRef } type =" text" placeholder =" Start
typing ..." />;
11 }
12
3 function ToggleButton () {
4 const [isOn , setIsOn ] = useState ( false );
5
6 return (
7 <button onClick ={() => setIsOn (! isOn)}>
8 {isOn ? 'ON ' : 'OFF '}
9 </button >
10 );
11 }
12
8 return (
9 <ThemeContext . Provider value ={{ theme , toggleTheme : () =>
setTheme ( theme === 'light ' ? 'dark ' : 'light ') }}>
10 { children }
11 </ ThemeContext .Provider >
12 );
15
13 }
14
15 function ThemeComponent () {
16 const { theme , toggleTheme } = useContext ( ThemeContext );
17 return (
18 <div style ={{ background : theme === 'light ' ? '#fff ' :
'#333', color : theme === 'light ' ? '#000 ' : '#fff ' }}>
19 <p> Current theme : { theme }</p>
20 <button onClick ={ toggleTheme }> Toggle Theme </ button >
21 </div >
22 );
23 }
24
3 function Form () {
4 const [name , setName ] = useState ('');
5
11 return (
12 <form onSubmit ={ handleSubmit }>
13 <input
14 type =" text"
15 value ={ name}
16 onChange ={(e) => setName (e. target . value )}
17 placeholder =" Enter name"
18 />
19 <button type =" submit ">Submit </ button >
20 </form >
21 );
22 }
23
16
5.9 Question 39: Dynamic List Rendering
Create a component to render a dynamic list.
1 import React from 'react ';
2
3 function TitleUpdater () {
4 const [count , setCount ] = useState (0);
5
10 return (
11 <div >
12 <p>Count: { count }</p>
13 <button onClick ={() => setCount ( count + 1)}>Increment </
button >
14 </div >
15 );
16 }
17
17
5.11 Question 41: Toggle Visibility
Create a component to toggle content visibility.
1 import React , { useState } from 'react ';
2
3 function ToggleContent () {
4 const [isVisible , setIsVisible ] = useState ( false );
5
6 return (
7 <div >
8 <button onClick ={() => setIsVisible (! isVisible )}>
9 { isVisible ? 'Hide ' : 'Show '} Content
10 </button >
11 { isVisible && <p>This is some content !</p >}
12 </div >
13 );
14 }
15
3 function DeletableList () {
4 const [items , setItems ] = useState (['Apple ', 'Banana ', 'Orange
']);
5
10 return (
11 <ul >
12 { items .map (( item , index ) => (
13 <li key ={ index }>
14 {item}
15 <button onClick ={() => deleteItem ( index )}>Delete
</ button >
16 </li >
17 ))}
18 </ul >
19 );
20 }
21
18
5.13 Question 43: Timer Component
Create a timer component using useEffect.
1 import React , { useState , useEffect } from 'react ';
2
3 function Timer () {
4 const [seconds , setSeconds ] = useState (0);
5
3 function MultiInputForm () {
4 const [form , setForm ] = useState ({ name: '', email : '' });
5
15 return (
16 <form onSubmit ={ handleSubmit }>
17 <input
18 type =" text"
19 name =" name"
20 value ={ [Link]}
21 onChange ={ handleChange }
22 placeholder =" Name"
23 />
24 <input
25 type =" email "
26 name =" email "
27 value ={ form. email }
19
28 onChange ={ handleChange }
29 placeholder =" Email "
30 />
31 <button type =" submit ">Submit </ button >
32 </form >
33 );
34 }
35
3 function ConditionalRender () {
4 const [isLoggedIn , setIsLoggedIn ] = useState ( false );
5
6 return (
7 <div >
8 <button onClick ={() => setIsLoggedIn (! isLoggedIn )}>
9 { isLoggedIn ? 'Log Out ' : 'Log In '}
10 </button >
11 { isLoggedIn ? <p>Welcome , User !</p> : <p> Please log in
.</p>}
12 </div >
13 );
14 }
15
6 Problem-Solving
6.1 Question 46: Second Largest Number
Write a function to find the second largest number in an array.
1 function secondLargest (arr) {
2 if (arr. length < 2) return null;
3 const sorted = [... new Set(arr)]. sort ((a, b) => b - a);
4 return sorted [1] || null;
5 }
6 console .log( secondLargest ([3 , 1, 4, 1, 5, 9, 2])); // Output : 5
20
1 function isBalanced (str) {
2 const stack = [];
3 for (let char of str) {
4 if (char === '(') stack .push(char);
5 else if (char === ') ') {
6 if (! stack .pop ()) return false ;
7 }
8 }
9 return stack. length === 0;
10 }
11 console .log( isBalanced ("((() ))")); // Output : true
21
6.5 Question 50: Reverse Words in a Sentence
Write a function to reverse words in a sentence.
1 function reverseWords ( sentence ) {
2 return sentence . split (' ').map(word => word. split (''). reverse ().
join ('')).join(' ');
3 }
4 console .log( reverseWords (" Hello World ")); // Output : " olleH dlroW "
10 return (
11 <div >
12 <input
13 type =" text"
14 value ={ filter }
15 onChange ={(e) => setFilter (e. target . value )}
16 placeholder =" Filter items ..."
17 />
18 <ul >
19 { filteredItems .map(item => (
20 <li key ={ [Link]}>{ [Link] }</li >
21 ))}
22 </ul >
22
23 </div >
24 );
25 }
26
23
3 return (n & (n - 1)) === 0;
4 }
5 console .log( isPowerOfTwo (16)); // Output : true
6 console .log( isPowerOfTwo (18)); // Output : false
24
1 const express = require ('express ');
2 const router = express . Router ();
3 const Product = require ( './ models /Product ');
4
5 router .get ('/ products /price ', async (req , res) => {
6 try {
7 const { min , max } = req. query ;
8 const products = await Product .find ({
9 price: { $gte: Number (min), $lte: Number (max) }
10 });
11 [Link]( products );
12 } catch ( error ) {
13 res. status (500) .json ({ message : 'Error fetching products ',
error });
14 }
15 });
16
25