School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,996 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,966 people online right now. Registration is fast and FREE... Join Now!




Interview Question

2 Pages V  1 2 >  

Interview Question

homemade-jam

24 Nov, 2008 - 09:59 AM
Post #1

Gabe's Nemesis
Group Icon

Joined: 17 Mar, 2008
Posts: 1,255



Thanked: 4 times
Dream Kudos: 50
My Contributions
I went to an interview the other day for a Computer Science course at university and they asked me this question:

How would you construct a program that would output the square of any given number without using multiplication?

Hint:
Spoiler:
Use the fact that the difference between the difference of squares is 2


I'm not entirely sure what the whole answer was smile.gif

User is offlineProfile CardPM
+Quote Post


KYA

RE: Interview Question

24 Nov, 2008 - 10:20 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Multiplication is simply fast addition. You could do it using only adding.

i.e

cpp

for(int i = 0; i < numGiven; i++)
{
square += numGiven;
}
cout << "Square is:" << square << endl;

cout << "KYA is awesome!";

User is online!Profile CardPM
+Quote Post

dbasnett

RE: Interview Question

24 Nov, 2008 - 10:27 AM
Post #3

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
i think i would've told them not to ask stupid questions.


User is offlineProfile CardPM
+Quote Post

AdamSpeight2008

RE: Interview Question

24 Nov, 2008 - 10:30 AM
Post #4

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,734



Thanked: 160 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
This one way I know, just using Shift and Add
CODE

Input n
x=n
d=n
t=0
while d<>0
x = x << 1
If (d And 1)=1 Then
  t += x
End If
d = d >> 1
End While
Output t

Takes: n log 2
Rather: n as in KYA's

On the ARM this only 9 Instruction
CODE

MOV   R1,R0
MOV   R2,R0
MOV   R0,#0
.w
BIC    R2,#1
ADDNE R0,R0,R1
MOV  R1,R1,ASL#1
MOV R2,R2,ASR#1
BNE w
MOV PC,R14


This post has been edited by AdamSpeight2008: 24 Nov, 2008 - 11:02 AM
User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

24 Nov, 2008 - 10:35 AM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
QUOTE(dbasnett @ 24 Nov, 2008 - 11:27 AM) *

i think i would've told them not to ask stupid questions.


How is it a stupid question? Programming is more then being able to hammer out solutions. It's all about problem solving, abstract thinking, innovation, and so on.
User is online!Profile CardPM
+Quote Post

AdamSpeight2008

RE: Interview Question

24 Nov, 2008 - 10:41 AM
Post #6

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,734



Thanked: 160 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
QUOTE(dbasnett @ 24 Nov, 2008 - 05:27 PM) *

i think i would've told them not to ask stupid questions.

"NEXT", Interviewer
or
"Would you like direction to the Train Station"
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo

RE: Interview Question

24 Nov, 2008 - 12:32 PM
Post #7

Not Your Ordinary Programmer
Group Icon

Joined: 21 Mar, 2008
Posts: 1,846



Thanked: 205 times
Dream Kudos: 500
Expert In: .NET

My Contributions
edited: I can't read.

This post has been edited by eclipsed4utoo: 24 Nov, 2008 - 12:35 PM
User is offlineProfile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 05:18 AM
Post #8

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
i was joking. please don't lecture me about programming. chances are that i wrote my first program before you were born. a better question
construct a program to raise each of these numbers (1,2,3,4) to each of these powers(0,1,2,3,4) without multiplying.




QUOTE(KYA @ 24 Nov, 2008 - 12:35 PM) *

QUOTE(dbasnett @ 24 Nov, 2008 - 11:27 AM) *

i think i would've told them not to ask stupid questions.


How is it a stupid question? Programming is more then being able to hammer out solutions. It's all about problem solving, abstract thinking, innovation, and so on.


This post has been edited by dbasnett: 25 Nov, 2008 - 05:43 AM
User is offlineProfile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 05:41 AM
Post #9

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
imho all of the answers given assumed restrictions not given. the only stated restriction was no multiply. so

CODE
        Dim theNumbers() As Integer = New Integer() {1, 2, 3, 4}
        Dim thePowers() As Integer = New Integer() {0, 1, 2, 3, 4}
        Dim theAnsw As Integer
        For tn As Integer = 0 To theNumbers.Length - 1
            For tp As Integer = 0 To thePowers.Length - 1
                theAnsw = CInt(Math.Pow(theNumbers(tn), thePowers(tp)))
                'or
                'theAnsw = CInt(theNumbers(tn) ^ thePowers(tp))
                Debug.WriteLine(theAnsw.ToString)
            Next
        Next


User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

25 Nov, 2008 - 10:53 AM
Post #10

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Joke wasn't funny.
User is online!Profile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 12:38 PM
Post #11

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
you are entitled to your opinion. your lecture wasn't funny either, IMHO.

This post has been edited by dbasnett: 25 Nov, 2008 - 12:40 PM
User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

25 Nov, 2008 - 01:27 PM
Post #12

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
QUOTE

How is it a stupid question? Programming is more then being able to hammer out solutions. It's all about problem solving, abstract thinking, innovation, and so on.


^ this?

It wasn't meant to be funny. Keep your "humor" to the lounge or to yourself.
User is online!Profile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 01:39 PM
Post #13

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
i found it extremely funny.
User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

25 Nov, 2008 - 01:44 PM
Post #14

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
I don't really know you or why you're jumping in my shit, but I am thoroughly amused.
User is online!Profile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 01:50 PM
Post #15

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
i thought it was the other way around. i made a joke, and you felt the need to get on the bully pulpit.


User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

25 Nov, 2008 - 01:55 PM
Post #16

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
My initial response is well formed, intelligent and not condescending in any manner. Your perception is your reality though.
User is online!Profile CardPM
+Quote Post

dbasnett

RE: Interview Question

25 Nov, 2008 - 02:15 PM
Post #17

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
it is preachy and arrogant. you obviously thought i needed a lecture about what programming is.
User is offlineProfile CardPM
+Quote Post

KYA

RE: Interview Question

25 Nov, 2008 - 02:20 PM
Post #18

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,491



Thanked: 508 times
Dream Kudos: 2875
Expert In: C, C++, Java

My Contributions
Your response implied that you have never been to an interview.
User is online!Profile CardPM
+Quote Post

Mike111

RE: Interview Question

25 Nov, 2008 - 02:43 PM
Post #19

New D.I.C Head
*

Joined: 19 Nov, 2008
Posts: 21



Thanked: 1 times
My Contributions
IPB Image
User is offlineProfile CardPM
+Quote Post

William_Wilson

RE: Interview Question

25 Nov, 2008 - 02:57 PM
Post #20

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,667



Thanked: 97 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
Getting back on topic and away from the childish banter.


The question was pretty good actually. Such a simple topic yet a roundabout answer is necessary. It tests a programmer's ability to not only think non-linearly, but to think on your feet in an interview; where most people would feel a little pressure which is to be expected with day to day deadlines.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:02AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month