由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道面试题
相关主题
Expedia工作机会dev&test (更新一下JD) (转载)【全职】Job Position Quantitative Trader & Junior Quant
写了一个Queens的backtrack 大牛帮我看看秒成渣!iPhone屏幕响应能力对比安卓设备
Job Opportunity如何 测量某个函数的运行时间?
两道很有意思的面试题目,大家议一议C++ 问题紧急求救
攒rp,发个G家面经一个搞统计的对C#的第一印象
【全职】Job Position Quantitative Trader & Junior QuantGo 1.5 这个 low latency GC 到底有多厉害?
相关话题的讨论汇总
话题: msg话题: messages话题: head话题: tail话题: window
进入JobHunting版参与讨论
1 (共1页)
c****s
发帖数: 241
1
被拒的不明不白,实在不甘心。哪位高手能不能指点一下?
// Implement in C++ class Window illustrating the concept of a "sliding tim
e window on a data stream":
// A window object holds continuously arriving messages of type Msg for n mi
llisecs after
// a message is received. After n milliseconds messages are purged.
// For the purpose of this exercise, Msg is defined like this:
struct Msg
{
int m_key; // all keys within a window are unique.
double m_value;
}
// Messages are continuously added by the user of the
h*********e
发帖数: 56
2
抛砖引玉,我觉得这个有点像circular buffer.
1) how to manage memory:
Maintain an array of Messages and HEAD and TAIL index. add from HEAD, remove
(when time expires) from TAIL. When HEAD + 1 == TAIL, it's full. When HEAD
== TAIL, it's empty. When HEAD or TAIL equals SIZE, reset to 0.
2) timer:
In the constructor, register timer signal handler, which expires after N
milliseconds. After that, in every one milliseconds, remove all messages
added in N milliseconds before, by TAIL++.
3) efficient interface functio
c****s
发帖数: 241
3
这个跟我的idea很像,但是后来就受到据信了,觉得很受伤。
我的idea就是用hash来存message,用ring array来存平均值。希望大侠们能指点一下
迷津

remove
HEAD

【在 h*********e 的大作中提到】
: 抛砖引玉,我觉得这个有点像circular buffer.
: 1) how to manage memory:
: Maintain an array of Messages and HEAD and TAIL index. add from HEAD, remove
: (when time expires) from TAIL. When HEAD + 1 == TAIL, it's full. When HEAD
: == TAIL, it's empty. When HEAD or TAIL equals SIZE, reset to 0.
: 2) timer:
: In the constructor, register timer signal handler, which expires after N
: milliseconds. After that, in every one milliseconds, remove all messages
: added in N milliseconds before, by TAIL++.
: 3) efficient interface functio

a******n
发帖数: 164
4
I also think circular buffer would be a solution:
struct DataSet
{
int nNumberOfEntries;
int iStart;
int iEnd;
uint32_t uKey;
Msg msgSet[MAX_NUMBER]; //MAX_NUMBER can be calculated
//by using max msg rate
float fpTotalSum;
};
void AddMessage(Msg *pMsg)
{
pMsg->m_nKey = uKey++;
memcpy(msgSet + iEnd++, pMsg, sizeof(Msg));

fpTotalSum += pMsg->value;
//check the head of the buffer, to make iStart
//points to Msg that it's still valid,
//and
c****s
发帖数: 241
5
thanks a lot for your input. I still have two concerns on this solution:
1) how to get average in the sliding windows.
2) timer is the most important condition. I don't think we can avoid it.
Which one do you think is better, hash table or binary search?

【在 a******n 的大作中提到】
: I also think circular buffer would be a solution:
: struct DataSet
: {
: int nNumberOfEntries;
: int iStart;
: int iEnd;
: uint32_t uKey;
: Msg msgSet[MAX_NUMBER]; //MAX_NUMBER can be calculated
: //by using max msg rate
: float fpTotalSum;

a******n
发帖数: 164
6
1) Since we already keeps the total sum and number of entries, it should be
easy to get avg.
2) Timer is very bad for high performance apps, basically all signals are
bad for high performance apps. Most of the cases, we may want to avoid it. (
for saving context switching) for example we can avoid it using another
wrapper struct:
struct WrapMsg
{
time_t ts; // timestamp, when the message is added to circular buffer.
Msg msg;
};
This way the head always has the oldest message. The latest m

【在 c****s 的大作中提到】
: thanks a lot for your input. I still have two concerns on this solution:
: 1) how to get average in the sliding windows.
: 2) timer is the most important condition. I don't think we can avoid it.
: Which one do you think is better, hash table or binary search?

h*********e
发帖数: 56
7
You are right. The timer is not necessary. We don't have to remove messages
at the EXACT time, as long as we don't use them if obsolete.

be
(

【在 a******n 的大作中提到】
: 1) Since we already keeps the total sum and number of entries, it should be
: easy to get avg.
: 2) Timer is very bad for high performance apps, basically all signals are
: bad for high performance apps. Most of the cases, we may want to avoid it. (
: for saving context switching) for example we can avoid it using another
: wrapper struct:
: struct WrapMsg
: {
: time_t ts; // timestamp, when the message is added to circular buffer.
: Msg msg;

1 (共1页)
进入JobHunting版参与讨论
相关主题
Job Opportunity如何 测量某个函数的运行时间?
两道很有意思的面试题目,大家议一议C++ 问题紧急求救
攒rp,发个G家面经一个搞统计的对C#的第一印象
【全职】Job Position Quantitative Trader & Junior QuantGo 1.5 这个 low latency GC 到底有多厉害?
【全职】Job Position Quantitative Trader & Junior QuantExpedia工作机会dev&test (更新一下JD) (转载)
秒成渣!iPhone屏幕响应能力对比安卓设备写了一个Queens的backtrack 大牛帮我看看
相关话题的讨论汇总
话题: msg话题: messages话题: head话题: tail话题: window