0% found this document useful (0 votes)
46 views2 pages

Firas Mock Interview Questions

The document discusses designing custom map and reduce functions in JavaScript. It explains that map transforms each item in a collection according to a function, while reduce cumulatively applies a function to reduce items to a single value. It provides examples of using a custom reduce to find the second largest number in an integer list.

Uploaded by

alirazza.10001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Firas Mock Interview Questions

The document discusses designing custom map and reduce functions in JavaScript. It explains that map transforms each item in a collection according to a function, while reduce cumulatively applies a function to reduce items to a single value. It provides examples of using a custom reduce to find the second largest number in an integer list.

Uploaded by

alirazza.10001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 1: Designing a Custom Map Function

Consider the `map` function, a fundamental concept in functional programming that transforms
each item in a collection according to a specified operation, resulting in a new collection with the
transformed items. How would you approach designing and implementing your version of the
`map` function from scratch in Python, JavaScript, or any other programming language you
prefer? Consider how your function will handle edge cases, such as empty collection. Describe
your implementation strategy and the thought process behind your design decisions.

mapFn = (collection, transformFunction) => newArray

Question 2: Designing a Custom Reduce Function


Moving beyond transformation to aggregation, the `reduce` function plays a critical role in
functional programming by cumulatively applying an operation to the items of a collection,
thereby reducing them to a single value. This function is instrumental in data processing tasks
such as summing values, finding maximums, or accumulating results in a specific structure.
Your task is to design and implement a custom version of the `reduce` function. Consider how
your function will handle edge cases, such as empty collections or the absence of an initial
accumulator value. Describe your implementation strategy and the thought process behind your
design decisions.

reduceFn = (collection, reducerFunction, initialValue) => singleValue

reducerFunction = (accumulator, currentValue) => newValue

● accumulator: This is a value that accumulates the results of each function call. It's like a
running total or combined result that gets updated each time the function is called.
● currentValue: This is the current element being processed in the array or collection.
● newValue: This is the new value of the accumulator after combining it with the current
value.

In essence, the reducer function takes two things — an ongoing total (accumulator) and the
current item (currentValue) — and combines them to form a new ongoing total. This process
repeats for each item in the collection, ultimately resulting in a single value that represents the
culmination of combining all items in the collection according to the specific logic defined in the
reducer function.
Bonus Question: Find the Second Largest Element

Problem Statement:
Using our custom reduce function, write code that finds the second largest element in a list of
positive integers.

Example Inputs and Outputs

1. Input: `[10, 5, 20, 8]`


Output: `10`

2. Input: `[25, 1, 25, 17, 23]`


Output: `23`

3. Input: `[3, 45, 75, 12, 75]`


Output: `45`

Note: Assume the list contains at least two unique positive integers.

You might also like