Problem Solving Flashcards
1
Q
How do you reverse a string in JavaScript?
A
javascript function reverseString(str) { return str.split('').reverse().join(''); }
This function splits the string into an array of characters, reverses the array, and joins it back into a string.
2
Q
Write a SQL query to find the second highest salary.
A
sql SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
This query selects the maximum salary from the employees table that is less than the maximum salary overall.
3
Q
What is the difference between synchronous and asynchronous code?
A
- Synchronous code runs sequentially and blocks execution.
- Asynchronous code runs in the background and uses callbacks, promises, or async/await.
Synchronous code waits for each operation to complete before moving on, while asynchronous code allows other operations to run while waiting for a response.
4
Q
Write a function to check if a string is a palindrome.
A
javascript function isPalindrome(str) { return str === str.split('').reverse().join(''); }
This function checks if the string is the same forwards and backwards.