<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[design question: display the highest x stock values from a stream]]></title><description><![CDATA[<p>Hello,</p>
<p>I want to implement the following coding question, but don't know how i should handle an update of the same stock value...like here: {&quot;Adobe&quot;, 12}, {&quot;Adobe&quot;, 13}</p>
<p>My approach was to create a min heap and i used a priority_queue therefore...</p>
<p>The task:<br />
We maintain stock prices of various companies. A stream of stock updates are coming in the form of ticker and value pair (example YHOO, 36.00). This value needs to be updated. We have a module of GUI that always displays top 5 stock prices at any given point of time. How would you maintain these values in memory?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;set&gt;
using namespace std;

typedef struct comparison {
	bool operator()(std::pair&lt;string,int&gt; &amp;lhs, std::pair&lt;string,int&gt; &amp;rhs) {
		return lhs.second &gt; rhs.second;
	}
}comparison;

typedef priority_queue&lt;std::pair&lt;string,int&gt;, vector&lt;std::pair&lt;string,int&gt;&gt;, comparison&gt; priority_q;

priority_q display_stocks_top(std::pair&lt;string,int&gt; &amp;ticker, int num) {
	static priority_q pq;

	if(pq.size() &lt; num) {
		pq.push(ticker);
	}
	else {
		std::pair&lt;string,int&gt; p = pq.top();

		if(ticker.second &gt; p.second) {
			pq.pop();
			pq.push(ticker);
		}
	}

	return pq;
}

int main() {
	// your code goes 
	std::vector&lt;std::pair&lt;string,int&gt;&gt; vec = {{&quot;Yahoo&quot;, 3}, {&quot;Google&quot;, 2}, {&quot;Microsoft&quot;, 10}, {&quot;VMware&quot;, 8}, {&quot;Adobe&quot;, 12}, {&quot;Adobe&quot;, 13}};
	const int display_num_elements = 2;

	for(int i = 0; i &lt; vec.size(); i++) {
		priority_q pq = display_stocks_top(vec[i], display_num_elements);

		cout &lt;&lt; &quot;top &quot; &lt;&lt; display_num_elements &lt;&lt; &quot; stock values: &quot; &lt;&lt; endl;

		while(!pq.empty()) {
			std::pair&lt;string,int&gt; p = pq.top();
			pq.pop();

			cout &lt;&lt; p.first &lt;&lt; &quot;...&quot; &lt;&lt; p.second &lt;&lt; endl;
		}

		cout &lt;&lt; &quot;\n&quot;;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/321887/design-question-display-the-highest-x-stock-values-from-a-stream</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 20:30:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/321887.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Nov 2013 23:16:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Wed, 27 Nov 2013 23:16:42 GMT]]></title><description><![CDATA[<p>Hello,</p>
<p>I want to implement the following coding question, but don't know how i should handle an update of the same stock value...like here: {&quot;Adobe&quot;, 12}, {&quot;Adobe&quot;, 13}</p>
<p>My approach was to create a min heap and i used a priority_queue therefore...</p>
<p>The task:<br />
We maintain stock prices of various companies. A stream of stock updates are coming in the form of ticker and value pair (example YHOO, 36.00). This value needs to be updated. We have a module of GUI that always displays top 5 stock prices at any given point of time. How would you maintain these values in memory?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;queue&gt;
#include &lt;set&gt;
using namespace std;

typedef struct comparison {
	bool operator()(std::pair&lt;string,int&gt; &amp;lhs, std::pair&lt;string,int&gt; &amp;rhs) {
		return lhs.second &gt; rhs.second;
	}
}comparison;

typedef priority_queue&lt;std::pair&lt;string,int&gt;, vector&lt;std::pair&lt;string,int&gt;&gt;, comparison&gt; priority_q;

priority_q display_stocks_top(std::pair&lt;string,int&gt; &amp;ticker, int num) {
	static priority_q pq;

	if(pq.size() &lt; num) {
		pq.push(ticker);
	}
	else {
		std::pair&lt;string,int&gt; p = pq.top();

		if(ticker.second &gt; p.second) {
			pq.pop();
			pq.push(ticker);
		}
	}

	return pq;
}

int main() {
	// your code goes 
	std::vector&lt;std::pair&lt;string,int&gt;&gt; vec = {{&quot;Yahoo&quot;, 3}, {&quot;Google&quot;, 2}, {&quot;Microsoft&quot;, 10}, {&quot;VMware&quot;, 8}, {&quot;Adobe&quot;, 12}, {&quot;Adobe&quot;, 13}};
	const int display_num_elements = 2;

	for(int i = 0; i &lt; vec.size(); i++) {
		priority_q pq = display_stocks_top(vec[i], display_num_elements);

		cout &lt;&lt; &quot;top &quot; &lt;&lt; display_num_elements &lt;&lt; &quot; stock values: &quot; &lt;&lt; endl;

		while(!pq.empty()) {
			std::pair&lt;string,int&gt; p = pq.top();
			pq.pop();

			cout &lt;&lt; p.first &lt;&lt; &quot;...&quot; &lt;&lt; p.second &lt;&lt; endl;
		}

		cout &lt;&lt; &quot;\n&quot;;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2368513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368513</guid><dc:creator><![CDATA[stock_coder]]></dc:creator><pubDate>Wed, 27 Nov 2013 23:16:42 GMT</pubDate></item><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Thu, 28 Nov 2013 07:58:39 GMT]]></title><description><![CDATA[<p>take a look at std::map. it might be the solution for everything you want:</p>
<pre><code>#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
int main()
{
	std::map&lt;std::string, unsigned&gt; Stocks;
	Stocks[&quot;Yahoo&quot;] = 3;
	Stocks[&quot;Google&quot;] = 2;
	Stocks[&quot;Microsoft&quot;] = 10;
	Stocks[&quot;VMware&quot;] = 8;
	Stocks[&quot;Adobe&quot;] = 12;
	Stocks[&quot;Adobe&quot;] = 13;
	for(std::map&lt;std::string, unsigned&gt;::const_iterator i = Stocks.begin(); i != Stocks.end(); i++)
	{
		std::cout &lt;&lt; i-&gt;first &lt;&lt; &quot; = &quot; &lt;&lt; i-&gt;second &lt;&lt; '\n';
	}
}
</code></pre>
<p>so if you try to access a non-existing with the subscript-operator, it will insert one and access that. but if it already exists, you'll just access the existing one. on the other hand, the keys have to be unique (except for the multi_map as the name tells), but thats anyways what you want i guess. the map is also sorted, so it will speed up the access (in case of arithmetical key values it will just compare the value, in case of strings as i did it in my example, it will compare lexicographically).</p>
<p>sorry for my bad english...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2368528</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368528</guid><dc:creator><![CDATA[asafasd]]></dc:creator><pubDate>Thu, 28 Nov 2013 07:58:39 GMT</pubDate></item><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Thu, 28 Nov 2013 10:33:27 GMT]]></title><description><![CDATA[<p>You can read the input into a map like that:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;sstream&gt;
#include &lt;boost/algorithm/string/trim.hpp&gt;

std::pair&lt;std::string, float&gt; extract_tickervalue( std::istream&amp; is )
{
	std::pair&lt;std::string, float&gt; rval;
	std::getline( is &gt;&gt; std::ws, rval.first, ',' );

	boost::trim_right(rval.first);

	is &gt;&gt; rval.second;
	return rval;
}

std::istream&amp; extract_all_tickervalues( std::istream&amp; is, std::map&lt;std::string, float&gt;&amp; map )
{
	for(;;)
	{
		auto pair = extract_tickervalue(is);
		if(is)
			map[pair.first] = pair.second;
		else break;
	}

	return is;
}

int main ()
{
	std::map&lt;std::string, float&gt; map{{&quot;Yahoo&quot;, 3},
	                                 {&quot;Google&quot;, 2},
	                                 {&quot;Microsoft&quot;, 10},
	                                 {&quot;VMware&quot;, 8},
	                                 {&quot;Adobe&quot;, 12}};
	std::istringstream stream{&quot;Adobe, 7.5 Yahoo, 4 Google, 6 Microsoft, 0&quot;};

	for(auto const&amp; pair : map)
		std::cout &lt;&lt; pair.first &lt;&lt; ':' &lt;&lt; pair.second &lt;&lt; '\n';
	std::cout &lt;&lt; '\n';

	extract_all_tickervalues(stream, map);

	for(auto const&amp; pair : map)
		std::cout &lt;&lt; pair.first &lt;&lt; ':' &lt;&lt; pair.second &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2368569</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368569</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 28 Nov 2013 10:33:27 GMT</pubDate></item><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Fri, 29 Nov 2013 02:04:22 GMT]]></title><description><![CDATA[<p>Hi, wouldn't it be more efficient to store only 5 stock values in memory?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2368802</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368802</guid><dc:creator><![CDATA[stock_coder]]></dc:creator><pubDate>Fri, 29 Nov 2013 02:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Fri, 29 Nov 2013 07:04:47 GMT]]></title><description><![CDATA[<p>stock_coder schrieb:</p>
<blockquote>
<p>Hi, wouldn't it be more efficient to store only 5 stock values in memory?</p>
</blockquote>
<p>thats what we do. as i already said:</p>
<p>asafasd schrieb:</p>
<blockquote>
<p>the keys have to be unique (except for the multi_map as the name tells)</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2368809</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2368809</guid><dc:creator><![CDATA[asafasd]]></dc:creator><pubDate>Fri, 29 Nov 2013 07:04:47 GMT</pubDate></item><item><title><![CDATA[Reply to design question: display the highest x stock values from a stream on Sat, 30 Nov 2013 06:14:51 GMT]]></title><description><![CDATA[<p>hi,</p>
<p>you should assume the map to be empty at the beginning...you inserting them all from the stream.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2369037</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2369037</guid><dc:creator><![CDATA[stock_coder]]></dc:creator><pubDate>Sat, 30 Nov 2013 06:14:51 GMT</pubDate></item></channel></rss>