Coding Interview

coding interview questions part 1

Pinterest LinkedIn Tumblr

Complex Number

A complex number is a combination of real and imaginary numbers. A+Bi is a complex number where A is the real number and B is the imaginary number.
A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit,
Example:-
3+4i
3 – real number
4 – imaginary number

Source Code

package com.dsacode.Probelms;

public class ComplexNumber {

	    private double r;
	    private double i;

	    public ComplexNumber() {
	    	r = 0.0;
	    	i = 0.0;
	    }

	    public ComplexNumber(double r, double i) {
	    	this.r = r;
	    	this.i = i;
	    }


	    public ComplexNumber add(ComplexNumber c2) {
	    	ComplexNumber sum = new ComplexNumber();
	    	sum.r = r + c2.r;
	    	sum.i = i + c2.i;
	    	return sum;
	    }
	    

	    public ComplexNumber multiply(ComplexNumber c2) {
	    	ComplexNumber mul = new ComplexNumber();
	    	mul.r = r*c2.r - i*c2.i;
	    	mul.i = r*c2.i + i*c2.r;
	    	return mul;
	    }

	    
	    public ComplexNumber subtract(ComplexNumber c2) {
	    	ComplexNumber sub = new ComplexNumber();
	    	sub.r = r - c2.r;
	    	sub.i = i - c2.i;
	    	return sub;
	    }
	    
	 

	    public String toString() {
	    	String s = "";
	    	s = s + r + "+" + i + "i";
	    	return s;
	    }
	    
	    
	public static void main(String[] args) {

		ComplexNumber c1, c2, c3,c4, c5;
		
		c1 = new ComplexNumber(8,6);
		c2 = new ComplexNumber(4,5);

		c3 = c1.add(c2);
		System.out.println("Add:" + c1 + " + " + c2 + " = " + c3);
		
		
		c4 = c1.subtract(c2);
		System.out.println("Subtract:"+ c1 + " - " + c2 + " = " + c4);

		c5 = c1.multiply(c2);
		System.out.println("Multiply:"+ c1 + " * " + c2 + " = " + c5);


	}

}
#include "stdafx.h"

#include < iostream >
#include < sstream >
#include < iomanip >
using namespace std;

class Complex
{
private:
	float real;
	float imag;
public:
	Complex() : real(0), imag(0){ }
	Complex(float r, float i)
	{
		 real=r;
		 imag=i;
	}
	Complex operator - (Complex c2)    
	{
		Complex temp;
		temp.real = real - c2.real;
		temp.imag = imag - c2.imag;
		return temp;
	}

	Complex operator + (Complex c2)
	{
		Complex temp;
		temp.real = real + c2.real;
		temp.imag = imag + c2.imag;
		return temp;
	}
	Complex operator * (Complex c2)
	{
		Complex temp;
		temp.real = real * c2.real;
		temp.imag = imag * c2.imag;
		return temp;
	}
	string output()
	{
		std::ostringstream oss;
		oss << fixed << setprecision(1);
		oss << real << "+" << imag <<"i";
		return oss.str();
	}
};

int _tmain(int argc, _TCHAR* argv[])
{

	Complex c1(8.0, 6.0), c2(4, 5), c3, c4, c5;
	c3 = c1+c2;
	cout << "Add:" << c1.output() << " + " << c2.output() << " = " << c3.output() << endl;

	c4 = c1-c2;
	cout << "Subtract:" << c1.output() << " - " << c2.output() << " = " << c4.output() << endl;

	c5 = c1*c2;
	cout << "Multiply:" << c1.output() << " * " << c2.output() << " = " << c5.output() << endl;

	return 0;
}

Output

Add:8.0+6.0i + 4.0+5.0i = 12.0+11.0i
Subtract:8.0+6.0i - 4.0+5.0i = 4.0+1.0i
Multiply:8.0+6.0i * 4.0+5.0i = 2.0+64.0i

Algorithm Explanation

Take two complex numbers.
Add first complex number real with second complex number real and add first complex number imaginary with second complex number imaginary. Return third complex number with addition value.
Subtract first complex number real with second complex number real and Subtract first complex number imaginary with second complex number imaginary. Return third complex number with Subtract value
Calculate the multiplication using the following formula
(8 + 6i) * (4 + 5i) = 8(4 + 5i) + 6i(4 + 5i)
= 32 + 40i + 24i + 30i2
= 32 + 64i + 30(-1)
= 32 + 64i -30
= 2 + 64i Answer
1 2 3 4 5

32 Comments

  1. Excellent post. I was checking continuously this blog and I’m impressed! Extremely helpful information particularly the last part 🙂 I care for such information much. I was seeking this certain information for a long time. Thank you and good luck.

  2. I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 certain. Any suggestions or advice would be greatly appreciated. Thanks

  3. The subsequent time I read a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to learn, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could repair if you werent too busy in search of attention.

  4. whoah this weblog is magnificent i love studying your posts. Keep up the great paintings! You understand, many individuals are looking around for this info, you can aid them greatly.

  5. magnificent points altogether, you simply gained a logo new reader. What may you recommend about your put up that you simply made a few days in the past? Any positive?

  6. My brother suggested I might like this blog. He was totally right. This post truly made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!

  7. Just about all of what you state is astonishingly legitimate and that makes me ponder why I hadn’t looked at this in this light previously. Your article truly did switch the light on for me personally as far as this issue goes. Nonetheless there is actually just one factor I am not too comfortable with so while I attempt to reconcile that with the core theme of your point, allow me observe what all the rest of your readers have to say.Very well done.

  8. Hey! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!

  9. With the whole thing that appears to be developing throughout this particular subject matter, all your points of view are actually fairly stimulating. However, I beg your pardon, but I do not give credence to your entire strategy, all be it exciting none the less. It seems to us that your opinions are actually not entirely validated and in actuality you are yourself not even entirely certain of your point. In any event I did appreciate reading through it.

  10. There are some interesting time limits on this article however I don’t know if I see all of them heart to heart. There is some validity however I’ll take maintain opinion until I look into it further. Good article , thanks and we would like more! Added to FeedBurner as effectively

  11. Hi! Someone in my Myspace group shared this website with us so I came to look it over. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Great blog and fantastic style and design.

  12. Aw, this was a really nice post. In concept I want to put in writing like this additionally – taking time and precise effort to make a very good article… but what can I say… I procrastinate alot and not at all seem to get something done.

  13. I believe that is among the such a lot significant info for me. And i’m glad reading your article. However want to observation on few basic things, The site style is great, the articles is truly excellent : D. Good job, cheers

  14. I do agree with all the ideas you have presented in your post. They’re very convincing and will certainly work. Still, the posts are too short for beginners. Could you please extend them a little from next time? Thanks for the post.

  15. Amazing blog! Do you have any helpful hints for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any tips? Cheers!

  16. you’re really a excellent webmaster. The web site loading velocity is incredible. It kind of feels that you’re doing any distinctive trick. Furthermore, The contents are masterwork. you’ve performed a excellent task in this topic!

  17. Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch since I found it for him smile Therefore let me rephrase that: Thanks for lunch!

  18. I enjoy what you guys are usually up too. This sort of clever work and exposure! Keep up the superb works guys I’ve incorporated you guys to blogroll.

  19. Thank you for any other great post. Where else may anybody get that type of info in such a perfect method of writing? I have a presentation next week, and I am at the look for such info.

  20. Hey there I am so excited I found your weblog, I really found you by accident, while I was looking on Askjeeve for something else, Nonetheless I am here now and would just like to say many thanks for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to read it all at the minute but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job.

  21. With havin so much content do you ever run into any issues of plagorism or copyright violation? My website has a lot of completely unique content I’ve either written myself or outsourced but it looks like a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help reduce content from being ripped off? I’d truly appreciate it.

  22. I do believe all of the ideas you’ve presented in your post. They’re really convincing and can certainly work. Still, the posts are very short for beginners. May you please extend them a bit from next time? Thanks for the post.

  23. Hey there! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to prevent hackers?

Write A Comment