JP Morgan Interview Question for Field Saless






Comment hidden because of low score. Click to expand.
1
of 1 vote

for ( it=xmap.begin(), int i=0; it != xmap.end(); it++, i++ )
{

if ( i % 2)
{

mymap.erase (it);

}
}

- topofmyhead July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

incorrect, code below works

#include <iterator>
#include <map>
#include <iostream>

typedef std::map<int,int>::iterator m_t;

int main()
{
    std::map<int,int> m;

    m[1] = 1; m[2] = 2; m[3] = 3; m[4] = 4;
    m[5] = 5; m[6] = 6; m[7] =7; m[8]=8;m[9]=9;
    m[10] = 10; m[11]=11;

    m_t it = m.begin();
    for(; it != m.end(); ++it) m.erase(it++);

    it = m.begin();
    for(; it != m.end(); ++it) {
	std::cout << it->first << " , " << it->second << std::endl;
    }
}

- joe August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This should be wrong since when you erase(it), it will automatically point to next element.
for ( it=xmap.begin(), int i=0; it != xmap.end(); i++ )
{

if ( i % 2)
{
mymap.erase (it);
} else
it ++;
}

- bowen November 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

TestMap::iterator iter = test.begin();
int index = 1;
while(iter != test.end())
{
if (index % 2 == 0) iter = test.erase(iter);
else ++iter;
index++;
}

- xiaofeng.ustc February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Void eraseFunction(map<int,int> &x)
{
x.clear();
}

- prime July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to erase every other elements, not every elements!

- Itcecsa July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Void eraseFunction(map<int,int> &x)
{
       for(map<int,int>::iterator it = x.begin(); it != x.end(); ++it)
       {
            x.erase(it++);
       }
       
}

- Ram July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void remove_mp_odd_index_elements(map<int,int>& a_map)
{
    if( a_map.size() <= 1 )
        return;

    int elems_to_erase = a_map.size() / 2;
    int k = 1;
    map<int,int>::iterator it;
    while( k <= elems_to_erase )
    {
        it = a_map.begin();
        for(int j = 1; j<=k; j++)
            it++;
        a_map.erase(it); // an erase invalidates the iterator
        k++;
    }
}

void remove_mp_even_index_elements(map<int,int>& a_map)
{
    if( a_map.size() == 1 )
    {
        a_map.clear();
        return;
    }
    else if( a_map.size() == 2 ){
        a_map.erase(a_map.begin());
        return;
    }

    int elems_to_erase = a_map.size() / 2 + 1;
    int k = 1;
    map<int,int>::iterator it;
    while( k <= elems_to_erase )
    {
        it = a_map.begin();
        for(int j = 1; j<k; j++)
            it++;
        a_map.erase(it); // an erase invalidates the iterator
        k++;
    }
}
# I think that an "erase" invalidates all iterators 
# attached on a container. Thus begin() and/or end must be 
# called in order to have a valid set of iterators and 
# remove the correct elements.

- Thanasis September 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

erasing an element of map in for loop erases every alternative element.

map<int, int> mymap;

for(map<int, int>::iterator it = mymap.begin(); it != mymap.end(); ++it )
{
mymap.erase(it);
}

ig mymap have 10 element, above code erases only 5

- naveen November 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong Answer. Dead loop.

- Anonymous October 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void eraseFunction(map<int,int> &x)
{
map<int,int>::iterator k;
for(map<int,int>::iterator it = x.begin(); it != x.end();)
{
k = it;
it++;
if(it==x.end()) {
cout <<"Erasing "<< " [" << (*k).first << ", " << (*k).second << "]" << endl;
x.erase(k);
break;
}

it++;
cout <<"Erasing "<< " [" << (*k).first << ", " << (*k).second << "]" << endl;
x.erase(k);

}

}


This will work....

- kapsi December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

map<int, int> t;
for (map<int, int>::iterator it = t.begin(); it != t.end(); )
{
t.erase(it++);
if(it == t.end())
break;
it++;
}

- Anonymous January 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <algorithm>
#include <set>

int main() {
	std::set<int> myset;	

	for (int i = 0; i < 10; ++i) myset.insert(i*10);	
	std::cout << "before " << std::endl;
	std::for_each(myset.begin(), myset.end(), [](int n) {std::cout << n << std::endl;  });

	std::set<int>::iterator it = myset.begin();
	int n = 0;
	while (it != myset.end()) {
		if (n % 2 == 1) {
			myset.erase(it++);
		}
		else {
			++it;
		}
		n++;
	}

	std::cout << "after " << std::endl;
	std::for_each(myset.begin(), myset.end(), [](int n) {std::cout << n << std::endl;  });

	std::cout << "press enter to continue ..." << std::endl;
	std::cin.get();
	return 0;
}

- Anonymous March 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use

m_map.clear();

- NoIdea December 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool must_delete = false;
		
		for (auto it = my.cbegin(); it != my.cend() ; /* no increment */)
		{
			if (must_delete)
			{
				my.erase(it++);    // or "it = m.erase(it)" since C++11
			}
			else
			{
				++it;
			}
			 
			must_delete=!must_delete;
		}

- Siddharth April 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool must_delete = false;
		
		for (auto it = my.begin(); it != my.end() /* not hoisted */; /* no increment */)
		{
			if (must_delete)
			{
				my.erase(it++);    // or "it = m.erase(it)" since C++11
			}
			else
			{
				++it;
			}
			 
			must_delete=!must_delete;
		}

- Siddharth April 08, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More