Amazon Interview Question for Software Engineer / Developers


Country: United States




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

That seems like a really language specific question. With Java you can always use reflection, with C++ you can just directly read the bytes, since the concept of a private member can more accurately be defined as a contract than an actual restriction in C++

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

+1  //access private member of an object without being friend
    +2  #include<iostream>
    +3  using namespace std;
    +4
    +5  class Base {
    +6          int i;
    +7          char j;
    +8          public:
    +9          Base(int one,char two):i(one),j(two)    {}
   +10  };
   +11  int main()
   +12  {
   +13          Base *b=new Base(1,'a');
   +14          void *v=(void*)b;
   +15          int m=*(int*)v;
   +16          cout<<"private member i="<<m<<endl;
   +17          return 0;
   +18
   +19  }

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

I am able to modify the private value of the class.
if you can get your hands on a pointer/reference to a private member, you have full access to that member.

#include<iostream>
using namespace std;

class demo
{
        private: int info;

        public:
                 demo()
                 {
                         info=10;
                 }

                 void print_info()
                 {
                       cout<<info;
                 }
};

int main()
{

     demo ob;
     int* ptr=(int*)&ob;

     *ptr=20;

     ob.print_info();

     return 0;
}

- Atanu October 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your code is changing the private variable but it will change the 1st variable only. but it is impossible to change the variable from 2nd on wards.

- Nila Kamal Nayak October 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in Java use reflection:
object.getClass().getDeclaredField("fieldName").set(object, newValue);

- engzizo October 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Check if we can create a new concrete class from the base class, and then modify the private data through a concreate class.

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

#include <iostream>
#include <cstdlib>
#include <cstring>

class X {
  int x;
  int y;
  char* p;
  public:
    X() : x(0), y(0), p(0) {
    }
    ~X() {
      if(p)
        free(p);
    }
    void SetP(const char* _p) {
      size_t len = strlen(_p);
      if(!p) {
        p = static_cast<char*> (malloc(len));
      } else {
        p = static_cast<char*> (realloc(p, len));
      }
      memcpy(p, _p, strlen(_p));
    }
    const char* GetP() {
      return p;
    }
};

class Y {
  public:
    int x;
    int y;
    char* p;
  public:
    Y() : x(0), y(0), p(0) {
    }
    ~Y() {
    }
};

int main(int argc, char* argv[]) {
  X x;
  Y* y = reinterpret_cast<Y*>(&x);
  // Y* y = (Y*) &x;
  x.SetP("My Value");

  std::cout << "V: " << y->p << std::endl;
}

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

Hope this works

#include <iostream>
#include <cstdlib>
#include <cstring>

class X {
  int x;
  int y;
  char* p;
  public:
    X() : x(0), y(0), p(0) {
    }
    ~X() {
      if(p)
        free(p);
    }
    void SetP(const char* _p) {
      size_t len = strlen(_p);
      if(!p) {
        p = static_cast<char*> (malloc(len));
      } else {
        p = static_cast<char*> (realloc(p, len));
      }
      memcpy(p, _p, strlen(_p));
    }
    const char* GetP() {
      return p;
    }
};

class Y {
  public:
    int x;
    int y;
    char* p;
  public:
    Y() : x(0), y(0), p(0) {
    }
    ~Y() {
    }
};

int main(int argc, char* argv[]) {
  X x;
  Y* y = reinterpret_cast<Y*>(&x);
  // Y* y = (Y*) &x;
  x.SetP("My Value");

  std::cout << "V: " << y->p << std::endl;
}

- Anonymous March 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If you know the order in which the data members are declared then you can calculate the offset from the first byte where the data would reside and merely cast it to the appropriate byte you're looking for.

- Ahmed Ahmed September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If you know the order in which the data members are declared then you can calculate the offset from the first byte where the data would reside and merely cast it to the appropriate byte you're looking for.

- Ahmed Ahmed September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Reliably...I wouldn't...Hackily I would copy and past the class except I would change it from a class to a structure and remove all that "public/private/protected" stuff. You can then just do some pointer casting and suddenly you've broken all the rules of good programming.

- Anonymous September 26, 2014 | 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