Microsoft Interview Question for Software Engineer / Developers






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

table{col}
Do a cross product on the values. Values that are *NOT* repeated will have just one row where both the values are same in the cross product. We, do a group by on the cross product and select only those rows where count(*) = 1. Comments?

select A.col from
table as A, table as B where
A.col = B.col group by
A.col having count(*) = 1

- Anonymous November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will retrieve unique col values. I think the problem is asking for values that are different from previous data only.

For e.g.
col { a , b , a } the solution should be { a, b } however the above query would give us just { b }

- Anonymous November 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think one could use Distinct here:

Select Distinct col
from A;

This would give you only unique values in the table...as mentioned above you would get { a,b } if the values in the col is { a,b,a }

- Anonymous November 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think both a and b wi;; be returned becoz b is different from a in { a,b } and a is different from b in {b,a}

so i think effectively its asking to find all rows where a change occurs in the column...

i dont know how to construct an SQL statement for this tho

- rohit December 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think both a and b wi;; be returned becoz b is different from a in { a,b } and a is different from b in {b,a}

so i think effectively its asking to find all rows where a change occurs in the column...

i dont know how to construct an SQL statement for this tho

- rohit December 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do not think this could be done in databases. Databases are represented as sets and each tuple is a unique entity. It is impossible to determine an order of elements in a table without any field that signifies an order such as timestamp, autoincrement number etc.

- AbhiM December 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we need to use cursors and then iterate through it outputting if the value if different from previous value stored in a variable

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

declare cur_tab1 cursor for
select * from tab1
declare @x int
declare @y int
open cur_tab1
fetch next from cur_tab1 into @x
while(@@fetch_status=0)
begin
fetch next from cur_tab1 into @y
if (@x=@y)
print @y
@x=@y
end

close cur_tab1
deallocate cur_tab1

- vishnu September 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using cursors! -- you just funked. The person who asked the question is an idiot (or likely a C# developer pretending to be a SQL developer -- it's a bad translation of a standard C# or Java question).

Declare @workingspace table(id int Identity(1,1), data varchar(max))
INSERT INTO @workingspace (data)
SELECT LastName from SalesLt.Customer
Select top 1 data from @Workingspace
UNION
Select A.data from @workingspace A
JOIN @workingspace B ON A.ID=B.Id-1

The above query may not return repeatable results. AbhiM is correct

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

select t2.col from (
select col,MAX(ID) id from
(
select ROW_NUMBER() over(PARTITION by col order by col) id ,col from testtab t1) as t1
group by col) as t2

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

We can achieve this by using LAG function in oracle. The query looks like this.

SELECT current_row
FROM ( SELECT column current_row, LAG(column,1,0) OVER() prev_row
FROM table
)
WHERE current_row <> prev_row;
WHERE

- Satish Reddy March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't know the exact syntax but it should be something like the following.

SELECT X.A.Col1
FROM
((SELECT A.Col1 FROM Table1 A)
JOIN
(SELECT B.Col1 FROM Table1 B)
ON
(A.Rownum = B.Rownum+1)) X
WHERE
X.A.Col1 <> X.B.Col1

- Punya June 04, 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