# interview | 📚 C/C++ | Learning library

## kandi X-RAY | interview Summary

## kandi X-RAY | interview Summary

extern "C" 的作用是让 C++ 编译器将 extern "C" 声明的代码当作 C 语言代码处理，可以避免 C++ 因符号修饰导致代码不能和C语言库中的符号进行链接的问题。.

### Support

### Quality

### Security

### License

### Reuse

### Top functions reviewed by kandi - BETA

Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of interview

## interview Key Features

## interview Examples and Code Snippets

```
def set(self, results, query):
"""Set the result for the given query key in the cache.
When updating an entry, updates its position to the front of the LRU list.
If the entry is new and the cache is at capacity, removes the o
```

```
def within_past_week(self, timestamp):
"""Return True if timestamp is within past week, False otherwise."""
...
```

```
def extract_max_priority_page(self):
"""Return the highest priority link in `links_to_crawl`."""
pass
```

## Community Discussions

Trending Discussions on interview

QUESTION

Recently in a job interview, they ask me "**how to solve Parallel Inheritance Hierarchies when we try to reuse code through inheritance**". I thought on **Aggregation** or **Composition**, but i was a little confused on making an example based on that.

So I decided to leave it pending to deepen concepts later, but after investigating it did not end up forming a precise answer to that question, could someone explain me a solution or an example to this?

...ANSWER

Answered 2022-Feb-27 at 14:51Parallel Inheritance Hierarchies makes many unnecessary classes and makes code very fragile and tightly coupled.
For example, we have class `Sportsman`

and its `Goal`

's.

QUESTION

So I'm preparing for a technical interview, and one of my practice questions is the Kth smallest number. I know that I can do a sort for O(n * log(n)) time and use a heap for O(n * log(k)). However I also know I can partition it (similar to quicksort) for an average case of O(n).

The actual calculated average time complexity should be:

I've double checked this math using WolframAlpha, and it agrees.

So I've coded my solution, and then I calculated the actual average time complexity on random data sets. For small values of n, it's pretty close. For example n=5 might give me an actual of around 6.2 when I expect around 5.7. This slightly more error is consistent.

This only gets worse as I increase the value of n. For example, for n=5000, I get around 15,000 for my actual average time complexity, when it should be slightly less than 10,000.

So basically, my question is where are these extra iterations coming from? Is my code wrong, or is it my math? My code is below:

...ANSWER

Answered 2022-Feb-27 at 08:40As you and others have pointed out in the comments, your calculation assumed that the array was split in half on each iteration by the random pivot, which is incorrect. This uneven splitting has a significant impact: when the element you're trying to select is the actual median, for instance, the expected size of the array after one random pivot choice is 75% of the original, since you'll always choose the larger of the two arrays.

For an accurate estimate of the expected comparisons for each value of `n`

and `k`

, David Eppstein published an accessible analysis here and derives this formula:

This is a very close estimate for your values, even though this assumes no duplicates in the array.

Calculating the expected number of comparisons for k from `1`

to `n-1`

, as you do, gives ** ~7.499 * 10^7** total comparisons when

`n=5000`

, or almost exactly **comparisons per call of Quickselect as you observed.**

`15,000`

QUESTION

I was introduced to an example interview question, where given a 2D array of characters and a word, find the word in the array, where the next character of the word is either to the right or below the previous character, and return a list of the co-ordinates of each character found.

I wrote the following code to solve this:

...ANSWER

Answered 2022-Feb-03 at 00:43I disagree with your search depth being O(Wlog(W)). In the worst case, where every intermediate letter (except the last) matches the word, your recursion will have a branching factor of 2, and will explore `2^W`

paths. Take, as an example, an extremely large grid, filled entirely with `A`

s, and a sole `B`

in the lower right corner, and a search word `AAA...AB`

. You can see how that would explode exponentially (with respect to W).

You are correct too, in that you'd ignore the smaller term, so overall I think the time complexity should be O(RC*2^W).

QUESTION

I'm currently studying for a technical interview and I'm just going through the leetcode grind. I came across a question that is apparently asked pretty frequently by the company I'm about to interview at so I attempted it. I couldn't quite get it so I looked to the solution and came across this solution.

...ANSWER

Answered 2021-Dec-19 at 00:30This happens because `prev`

's reference is being pointed by `res`

when you do `res=[prev]`

, basically the address where the actual `prev`

array is stored is pointed, as the prev updates, it also show changes in res.

QUESTION

I had an interview recently and was asked to solve the following problem using Python:

Write a function:

...ANSWER

Answered 2021-Sep-16 at 02:01Not the most elegant. This assumes that e.g. -3 is single digit.

This algorithm will play by the rules set. Considering the constraints:

- The number is
**-10,000**to 10,000 - There is
**at least one element**which satisfies the condition

Then we can just guard all numbers beyond 9 to be the lowest -10,001 so that they are not considered to be max. We don't need to care for numbers lower than -9 as we are sure there is at least 1 element (that is a single digit whether positive or negative, either way greater than those numbers lower than -9) that will satisfy the condition.

QUESTION

During the job interview, I was asked how to optimize a component in React.

So I answered with an example of using **useCallback** and **useMemo** hooks.

Because whenever a components re-renders, functions and variables declared inside of the component are newly created which results in those newly created values occupying memory space.

However, with a help of those two hooks, it is possible to save memory because using **useCallback** and **useMemo** prevents creating the function and variable again and again by saving and reusing the previous result, as long as a state inside the dependency array is not changed during the component re-rendering.

So far, this was my answer. But the interviewer said **useCallback** does not prevent creating a function. Even if **useCallback** is used, a function is still created.

I do not get this part. How is the function created again inside the useCallback hook?

Is there something I'm missing about **useCallback** and **useMemo**?

ANSWER

Answered 2021-Sep-02 at 12:56**Here I made an example so you can see**

`useCallback`

in action:In the example bellow: `useCallback`

will prevent newly `changeWithUseCallback`

re-create every new render, result that `Child1`

never gets re-render because it's props (changeWithUseCallback) never re-created.

Otherwise, in the `Child2`

component, its prop (changeWithoutUseCallback) is always re-created during renders, so the component itself will re-render.

**Please note that React.memo will skip rendering the component if its props have not changed**

So I would say that you are right about `useCallback`

that it will keep the function/variable reference during renders, or `prevent creating new function/variable`

during renders.

QUESTION

Post API format

...ANSWER

Answered 2021-Sep-01 at 08:31This is a matter of async function. It returns `promise`

object (refrence):

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

As `makePostRequest`

is async function there is a need to use some asynchornous construction, like `than`

or `await`

, to get the results.

I think the easiest should be to correct `reply`

assignment and flowing lines to :

EDIT:

`return`

statment added to aviod `Error: No rersponse has been sent`

QUESTION

This is a problem I was asked to solve during an interview, but the code I implemented during interview cannot pass all test cases, the problem is just as title said, given N and T (T >= N), which are initial floor and target floor respectively, one can move to current floor + 1, current floor - 1 or 2 * current floor in one minute, what's the minimum time need to reach the target? I think it's a typical DP problem, this is how I did in interview

...ANSWER

Answered 2021-Aug-05 at 14:41An easy way is to start from the target `T`

and to found how many iterations we need to go down to initial value `N`

. Here, the allowed operations are +1, -1 and division by 2.

The key point is that division by 2 is only allowed for even value of `T`

. Moreover, if `T`

is even effectively, then it seems clear that division by 2 is the road to take, except if `T`

is near enough to `N`

. This little issue is solved by comparing `1 + nsteps(N, T/2)`

with `T - N`

.

If `T`

is odd, we must have to compare `nsteps(N, T-1)`

with `nsteps(N, T+1)`

.

Last but not least, if `T`

is less than `N`

, then the number of steps is equal to `N - T`

.

Complexity: ??

Here is a simple C++ implementation to illustrate the algorithm. It should be easy to adapt it in any language.

QUESTION

I recently came across an interview question which although had an immediately obvious solution, I struggled to find a more efficient one.

The actual question involved counting numbers from `a`

to `b`

(up to `2^64`

) which satisfied having either the digit `6`

or `8`

, but not both. They called it a 'lucky number'. So for example:

ANSWER

Answered 2021-Jun-30 at 03:55I would say you are at the right track. The gut feeling is that dealing with the `a`

and `b`

separately is easier. Making a function `count_lucky_numbers_below(n)`

allows

QUESTION

I'm using Active Job with sidekiq adapter in Rails 6.1.3.2

Strangely, custom service singleton methods are lost when they're called in Active Job.

**notify_message_event_job.rb**

ANSWER

Answered 2021-Jul-06 at 07:01Seemed like it was an issue with zeitwerk autoloading mode which was introduced as default in Rails 6

I temporarily opted out in config/environments/production.rb:

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

## Vulnerabilities

No vulnerabilities reported

## Install interview

## Support

## Reuse Trending Solutions

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

Find more librariesStay Updated

Subscribe to our newsletter for trending solutions and developer bootcamps

Share this Page