9.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 66edd4bc0ce399f475ad6f9b | JavaScript Problem Solving and Algorithmic Thinking Quiz | 8 | quiz-javascript-problem-solving-and-algorithmic-thinking |
--description--
Answer all of the questions below correctly to pass the quiz.
--quizzes--
--quiz--
--question--
--text--
The Fibonacci sequence is a series where each number is the sum of the two preceding ones, starting with 0 and 1. The sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21... and so on. What is the best approach to calculate nth number in the sequence?
--distractors--
Use the formula fib(n) = fib(n-1) * fib(n-2).
Use recursion with memoization to avoid recalculating subproblems where fib(n) = (n-1) + (n-2).
Use a for loop to calculate iteratively where fib(n) = fib(n-1) + (n-2).
--answer--
Use recursion with memoization to avoid recalculating subproblems where fib(n) = fib(n-1) + fib(n-2).
--question--
--text--
A binary string, such as "1011", represents a number in base-2, where each digit corresponds to a power of 2. What process can be used to convert this binary string into its decimal equivalent?
--distractors--
Use the parseInt() function with base 10.
Convert the string to an array, then sum the digits using bitwise operations.
Use the Number() function directly with a binary string.
--answer--
Multiply each character by powers of 2 and sum them up.
--question--
--text--
Which of the following algorithm will move all zeroes in an array to the end while keeping the order of other elements unchanged? For example, given [0, 1, 0, 3, 12], return [1, 3, 12, 0, 0].
--distractors--
Sort the array in decreasing order and place zeros at the start.
Sort the array in ascending order.
Use recursion to remove all zeros and add them to the front.
--answer--
Use a two-pointer technique to swap non-zero elements with zero elements.
--question--
--text--
Given a sentence, such as "I am a frontend engineer", what approach can be taken to reverse the order of the words, producing the output "engineer frontend a am I"?
--distractors--
Split the string into words using split(" "), reverse the array, and join it back into a string using join("_").
Sort the words alphabetically and return the result.
Use regular expressions to reverse each word.
--answer--
Split the string into words using split(" "), reverse the array, and join it back into a string using join(" ").
--question--
--text--
Given a string, what approach would be used to find the first non-repeating character? For example, in the string "codecamp", it would be 'o'.
--distractors--
Sort the string and check for consecutive duplicates.
Convert the string to an array, then filter for unique elements.
Use a regular expression to find the first non-repeat.
--answer--
Iterate over the string twice, once to count number of occurrences and once to find the first unique character.
--question--
--text--
What approach can be used to encode a string by counting consecutive repeated characters? For example, given the input "mississippi", the output should be "m1i1s2i1s2i1p2i1".
--distractors--
Use regular expressions to match repeated characters.
Use map() to map consecutive characters to the output.
Sort the string and count occurrences.
--answer--
Iterate through the string, counting consecutive occurrences of each character while building the answer.
--question--
--text--
What approach can be used to reverse the digits of an integer? For example, given the input 1234, the output should be 4321.
--distractors--
Use bitwise operations to reverse the digits.
Convert the integer to a binary number and reverse it.
Use the reverse() method directly on the integer.
--answer--
Use a loop to extract digits from the integer and reconstruct the reversed integer.
--question--
--text--
What approach would be used to generate Pascal's Triangle up to n rows? For example, for n = 3, the result would be [[1], [1, 1], [1, 2, 1]].
--distractors--
Generate rows by using a function that outputs prime numbers and maps them to each row.
Use a loop to generate each row by multiplying adjacent elements from the previous row.
Use Math.random() to generate the rows.
--answer--
Use a loop to generate each row by adding adjacent elements from the previous row.
--question--
--text--
Which algorithm is most efficient for sorting an array of size n containing numbers from 1 to n?
--distractors--
Bubble Sort
Merge Sort
Quick Sort
--answer--
Cyclic Sort
--question--
--text--
Which algorithmic approach is best for solving the problem of finding the minimum number of coins for a given amount when only one type of coin is provided?
--distractors--
Dynamic Programming
Divide and Conquer
Backtracking
--answer--
Greedy Algorithm
--question--
--text--
Which of the following problems can be solved efficiently using the two-pointer technique?
--distractors--
Sorting an array.
Traversing a tree level by level.
Finding the middle element of a linked list.
--answer--
Finding two numbers in a sorted array that sum up to a target value.
--question--
--text--
Given a Roman numeral string, convert it to its corresponding integer value. For example, "IX" should return 9.
--distractors--
Parse the string from left to right and add the values of individual numerals.
Parse each character and multiply by its index.
Use the parseInt() function on the Roman numeral string.
--answer--
Parse the string from right to left, adding the values of individual numerals and subtracting when a smaller numeral comes before a larger numeral.
--question--
--text--
How can one check if a given string is a palindrome (the same forwards and backwards), for example "madam"?
--distractors--
Use Slow and Fast Pointer.
Compare it to its reverse after sorting the string.
Use regular expressions to remove all non-alphabetic characters and check equality.
--answer--
Compare the string to its reverse.
--question--
--text--
Given a number n, write a function that prints numbers from 1 to n. For multiples of 3, print "Fizz" for multiples of 5, print "Buzz" and for multiples of both 3 and 5, print "FizzBuzz". What is the simplest approach to this?
--distractors--
Use Math.random() to generate numbers between 1 and n and check the result.
Use a for loop and check divisibility in-order of 3, 5 and 15 with if conditions or switch.
Create a recursive function that checks divisibility by 3 and 5.
--answer--
Use a for loop and check divisibility in-order of 15, 5 and 3 with if conditions or switch.
--question--
--text--
The Caesar cipher is a simple encryption technique where each letter in a string is shifted forward by a fixed number of positions in the alphabet. How would you shift all letters in a string by 3 positions?
--distractors--
Subtract 3 from the ASCII value of each character.
Replace each vowel with the next consonant.
Add 3 to the ASCII value of each character, but only for uppercase letters.
--answer--
Add 3 to the ASCII value of each character, wrapping around for 'z' and 'Z'.
--question--
--text--
Anagrams are words formed by rearranging letters of another word. How can one determine if two strings are anagrams?
--distractors--
Compare total number of letters in both strings.
Reverse one string and compare with the other.
Use regular expressions to remove duplicate letters and check equality.
--answer--
Sort both strings and check if they are equal.
--question--
--text--
Which of the following is the correct approach to check if a given number is prime?
--distractors--
Check if the number is divisible by 2 and 3. If not, it's prime.
Check if the number is divisible by any odd number between 3 and the square root of the number.
Convert the number to binary and check if it has exactly two 1s.
--answer--
Loop from 2 to up to the square root of the prime candidate, and make sure none of the number divides it evenly.
--question--
--text--
Which of the following describes the correct approach to find the greatest common divisor (GCD) of two numbers?
--distractors--
Find the average of the two numbers and round to the nearest integer.
Multiply the two numbers together and then divide by their least common divisor.
Add the two numbers together and divide by 2.
--answer--
Use recursion where gcd(a, b) = gcd(b, a % b) where the base case would be to return first parameter when second parameter is zero.
--question--
--text--
The factorial of a non-negative integer n (denoted n!) is the product of all positive integers from 1 to n, with 0! = 1. Which of the following approaches can be used to calculate the factorial of a given number n?
--distractors--
Add the numbers from 1 to n.
Raise n to the power of n-1.
Find the prime factorization of n and then multiply the prime factors together.
--answer--
Multiplying numbers from 1 to n in a loop.
--question--
--text--
Which of the following is the most efficient approach to search for a given integer within a distinct, sorted array of integers?
--distractors--
Linear Search.
Sort the array and search using Linear Search.
Create a hash table with the array elements and check for the given integer.
--answer--
Binary Search.