MS的面试问题 [英] An interview question by MS

查看:57
本文介绍了MS的面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一次采访中,我被问到:


定义一个用一些标记拆分字符串的函数。


我说让我函数返回一个字符串数组。


他问我怎么知道数组大小。

是的,我必须遍历字符串才能找到大小数组是第一次用于


并且当实际进行第二次

时间分割时遍历字符串。


我认为这可能不是正确的答案。


我应该如何处理这种情况?


我发现标准库中有一个'strtok(s,ct)''函数。

序列中的第一个cll有一个非NULL s。

每个后续调用,由空值s表示。

strtok在没有找到更多令牌时返回NULL。


为什么这个函数确实这样定义? />
为什么不直接返回字符串数组?

也许这是正确答案?

In an interview, I was asked:

Define a function to split a string with some token.

I said let the function return a string array.

He asked me how do i know the array size.
Yes, I have to traverse the string to find how large the array is for
the first time.
And traverse the string when actually doing the split for the second
time.

I think this is maybe not the correct answer.

How should i handle such situation?

I find there is a ''strtok(s, ct)'' function in the standard library.
The first cll in a sequence has a non-NULL s.
Each subsequent call, indicated by a NULL value of s.
strtok returns NULL when no further token is found.

Why indeed this function defined this way?
Why not return a string array directly?
Maybe this is the correct answer??

推荐答案

2007年11月6日星期二下午1:50 Lambda< st ********* @ gmail.comwrote in

article< 11 ********************** @ v23g2000prn.googlegroups .com>:
On Tuesday 06 Nov 2007 1:50 pm Lambda <st*********@gmail.comwrote in
article <11**********************@v23g2000prn.googlegroups .com>:

在一次采访中,我被问到:


定义一个用一些标记拆分字符串的函数。
In an interview, I was asked:

Define a function to split a string with some token.



标准库strtok执行此操作,但如果你要重新发明

这个功能你可以做得更好。

The Standard library strtok does this, but if you''re going to reinvent
the functionality you can do better.


我说让函数返回一个字符串数组。
I said let the function return a string array.



这是一种可能性。

This is one possibility.


他问我怎么知道阵列大小。

是的,我必须遍历字符串,以便第一次找到数组大小为


并在实际执行时转换字符串拆分第二个

时间。


我认为这可能不是正确答案。
He asked me how do i know the array size.
Yes, I have to traverse the string to find how large the array is for
the first time.
And traverse the string when actually doing the split for the second
time.

I think this is maybe not the correct answer.



没有正确的回答。除非出现明显错误的方法,否则每个设计可能适合某种情况。 strtok()的设计是

一种可能性,但正如我所说,如果你是从头开始写作,你可以改进它。


一般情况下,除了修改原始字符串(如strtok(),

)外,还有两种方法。一个是分裂函数分配

内存本身并返回令牌。在这种情况下,调用者通常需要释放内存,但它允许有效使用内存(?)。

。另一种方法是让调用者

提供一个缓冲区来存储令牌。这样可以轻松地重复使用内存。两种方法也可以组合使用。


还有其他不太有利的选项,比如使用静态和

全局数组等,通常是糟糕的设计,特别是对于库

函数。

There is no "the correct" answer. Barring obviously wrong methods, each
design may be appropriate to a certain situation. strtok() design is
one possibility, though as I noted, you can improve upon it if you are
writing from scratch.

In general, other than modifying the original string like strtok(),
there are two methods. One is for the splitting function to allocate
memory itself and return the token. In this case the caller is usually
left with the responsibility of freeing the memory, but it allows for
efficient use of memory(?). The other method is to have the caller
provide a buffer to store the token into. This allows for memory to be
reused easily. Both methods can also be combined.

There are also other less favourable options like using static and
global arrays etc., usually bad design, particularly for a library
function.


我该如何处理这种情况?
How should i handle such situation?





面试期间,他们是否要求您编写该功能?


通常最好的一般答案是讨论

各种方法的优缺点。它表明你知道的主题是

实际上是一个问题,而不仅仅局限于strtok()。

Did they ask you to write the function then and there during the
interview?

Usually the best general answer is to discuss the pros and cons of
various methods. It shows that you are aware of the subject which is
actually a problem not just confined to strtok().


我发现有标准库中的''strtok(s,ct)''函数。

序列中的第一个cll具有非NULL s。

每次后续调用,由NULL值表示。

strtok在没有找到更多令牌时返回NULL。


为什么这个函数确实这样定义了?
I find there is a ''strtok(s, ct)'' function in the standard library.
The first cll in a sequence has a non-NULL s.
Each subsequent call, indicated by a NULL value of s.
strtok returns NULL when no further token is found.

Why indeed this function defined this way?



您应该向原始开发人员询问他们是谁。

You should ask the original developers whoever they were.


为什么不直接返回字符串数组?

也许这是正确答案?
Why not return a string array directly?
Maybe this is the correct answer??



正如我所说,没有一个正确的答案,尽管有很多错误的

答案。

As I said there is no one correct answer, though there are many wrong
answers.


文章< 11 ********************** @ v23g2000prn.googlegroups .com>,

Lambda< st ********* @ gmail.comwrote:
In article <11**********************@v23g2000prn.googlegroups .com>,
Lambda <st*********@gmail.comwrote:

>他问我怎么知道数组大小。
是的,我必须遍历字符串,以便第一次找到数组的大小。
并在第二次实际进行拆分时遍历字符串。
>He asked me how do i know the array size.
Yes, I have to traverse the string to find how large the array is for
the first time.
And traverse the string when actually doing the split for the second
time.



这是一种合理的方式。但是由于你将不得不动态分配数组,你最初可以分配一个小的

,并在需要时调用realloc()来增长它。实际上,你最初可以分配1个条目并且每次调用realloc(),

依靠库来实现合理的realloc()。

That''s a plausible way to do it. But since you are going to have to
dynamically allocate the array, you could allocate a small size
initially and call realloc() to grow it when needed. In fact, you
could allocate 1 entry initially and call realloc() every time,
relying on the library to have a reasonable realloc() implementation.


>我发现标准库中有一个'strtok(s,ct)''函数。
序列中的第一个cll有一个非NULL s。
每个后续调用,由空值s表示。
strtok在没有找到更多令牌时返回NULL。

为什么这个函数确实以这种方式定义? />为什么不直接返回字符串数组?
也许这是正确的答案?
>I find there is a ''strtok(s, ct)'' function in the standard library.
The first cll in a sequence has a non-NULL s.
Each subsequent call, indicated by a NULL value of s.
strtok returns NULL when no further token is found.

Why indeed this function defined this way?
Why not return a string array directly?
Maybe this is the correct answer??



它具有不分配内存的优点,这可能很有用。

它有很大的缺点,它保持隐藏状态 -

字符串中的当前位置 - 所以你不能用不同的字符串交换到

strtok()的调用。一些实现有一个版本

strtok_r()带有一个额外的参数来避免这种情况。


- Richard

-

应考虑在一些字母表中需要多达32个字符

- 1963年的X3.4。

It has the advantage of not allocating memory, which may be useful.
It has the substantial disadvantage that it keeps hidden state - the
current position in the string - so you can''t interleave calls to
strtok() with different strings. Some implementations have a version
strtok_r() with an extra argument to avoid this.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


11月6日凌晨2:20,Lambda< stephenh ... @ gmail.comwrote:
On Nov 6, 2:20 am, Lambda <stephenh...@gmail.comwrote:

在一次采访中,我被问到:


定义一个用一些标记分割字符串的函数。


我说让函数返回一个字符串数组。


他问我怎么知道数组的大小。

是的,我必须遍历字符串,以便第一次找到数组大小为


并在第二次实际进行拆分时遍历字符串

时间。


我认为这可能不是正确的答案。


我应该如何应对这种情况?


我发现标准库中有一个'strtok(s,ct)''函数。

序列中的第一个cll有一个非NULL s。

每个后续调用,由空值s表示。

strtok在没有找到更多令牌时返回NULL。


为什么事实上这个函数是这样定义的吗?

为什么不直接返回字符串数组?

也许这是正确答案?
In an interview, I was asked:

Define a function to split a string with some token.

I said let the function return a string array.

He asked me how do i know the array size.
Yes, I have to traverse the string to find how large the array is for
the first time.
And traverse the string when actually doing the split for the second
time.

I think this is maybe not the correct answer.

How should i handle such situation?

I find there is a ''strtok(s, ct)'' function in the standard library.
The first cll in a sequence has a non-NULL s.
Each subsequent call, indicated by a NULL value of s.
strtok returns NULL when no further token is found.

Why indeed this function defined this way?
Why not return a string array directly?
Maybe this is the correct answer??



我发现MS面试问题的意图非常不可取,因为
引发了C响应。当然,所需的响应是创建和

以某种方式返回一个C ++容器对象。


I find it highly improbably that an MS interview question was intended
to elicit a C response. Surely the desired response was to create and
return a C++ container object in some fashion.


这篇关于MS的面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆