Bloomberg面接問題05032012[1]


Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
適当に書いて、読めばいいです.主な思想はキューを使うことで、callになったら入队して、いっぱいになったら头を出して、キューはclock_を保存しますtデータ.
#include <time.h>
#include <queue>

std::queue<clock_t> calls;

const int X = 10;
const int T = 80;

bool called()
{
	bool wasCalled = false;

	clock_t curTime = clock();
	clock_t oldTime = curTime - T * CLOCKS_PER_SEC;

	if(calls.size() == X)
	{
		if(calls.front() >= oldTime)
		wasCalled = true;

		calls.pop();
	}

	calls.push(curTime);
	return wasCalled;
}

int main()
{
	for(int i = 0; i < 10; i++)
		called();

	bool b = called();

	if(b)
		printf("YES");

	return 0;
}