You have been given an array of size 2n+1 that have n pair of integers(can be +ve, -ve or 0) and one unpaired element.
How would you find the unpaired element.
During a phone interview someone asked the question, "When looking at code, what the first thing that stands out to let you know its from a good/experienced developer".
I believe my answer was sufficient, just thought it was a bit strange so Im curious how some of you would answer?
Inspired by Raymond Chen's post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I'd like to see some real world stuff.
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]
Becomes:
[3][9][5][1]
[4][0][6][2]
[5][1][7][3]
[6][2][8][4]
Update: Nick's answer is the most straightforward, but is there a way to do it better than n^2? What if the matrix was 10000x10000?
Is it possible to compute the number of different elements in an array in linear time and constant space? Let us say it's an array of long integers, and you can not allocate an array of length sizeof(long).
P.S. Not homework, just curious. I've got a book that sort of implies that it is possible.
This is an interview question from google. I am not able to solve it by myself. Can somebody shed some light?
Write a program to print the sequence of keystrokes such that it generates the maximum number of character 'A's. You are allowed to use only 4 keys: A, Ctrl+A, Ctrl+C and Ctrl+V. Only N keystrokes are allowed. All Ctrl+ characters are considered as one keystroke, so Ctrl+A is one keystroke.
For example, the sequence A, Ctrl+A, Ctrl+C, Ctrl+V generates two A's in 4 keystrokes.
Ctrl+A is Select All
Ctrl+C is Copy
Ctrl+V is Paste
I did some mathematics. For any N, using x numbers of A's , one Ctrl+A, one Ctrl+C and y Ctrl+V, we can generate max ((N-1)/2)2 number of A's. For some N M, it is better to use as many Ctrl+A's, Ctrl+C and Ctrl+V sequences as it doubles the number of A's.
The sequence Ctrl+A, Ctrl+V, Ctrl+C will not overwrite the existing selection. It will append the copied selection to selected one.
Recenty I was asked this interview question:
There is a server which receives millions of requests every day. Design an API for finding out hits in the last one minute, in the last 10 minutes etc. What should be the algorithm and design to implement it efficienly.
I want to know the ideas on this.
I have two columns as company and product.
I use the following query to get the products matching particular string...
select id,(select name from company where product.cid=company.id) as
company,name,selling_price,mrp from product where name like '$qry_string%'
But when i need to list products of specific company how can i do?
i tried the following but in vein
select id,(select name from company where product.cid=company.id) as
company,name,selling_price,mrp from product where company like '$qry_string%'
Help me
Say you have a linked list structure in Java. It's made up of Nodes:
class Node {
Node next;
// some user data
}
and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.
What's the best way of writing
boolean hasLoop(Node first)
which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a finite amount of space and a reasonable amount of time?
Input : {5, 13, 6, 5, 13, 7, 8, 6, 5}
Output : {5, 5, 5, 13, 13, 6, 6, 7, 8}
The question is to arrange the numbers in the array in decreasing order of their frequency, preserving the order of their occurrence.
If there is a tie, like in this example between 13 and 6, then the number occurring first in the input array would come first in the output array.
I'm having a hard time wrapping my head around non-static nested classes in Java. Consider the following example, which prints "Inner" and then "Child".
class Outer {
class Inner {
Inner() { System.out.println("Inner"); }
}
}
public class Child extends Outer.Inner {
Child(Outer o) {
o.super();
System.out.println("Child");
}
public static void main(String args[]) {
new Child(new Outer());
}
}
I understand that instances of Inner always have to be associated with an Outer instance, and that that applies to Child too since it extends Inner. My question is what the o.super() syntax means - why does it call the Inner constructor?
I've only seen a plain super(args) used to call the superclass constructor and super.method() to call the superclass version of an overridden method, but never something of the form instance.super().
Well, I have an interview for my first internship, it will also be my first interview that is not customer service related. What kind of things should I expect to be asked? Any advice? It is over the phone so it cant be too technical(I think). All help is appreciated!
i have a log file which maintains source entry for each page.all the pages share the common file.
source means from what page did user arrive on the target page. I want to find the most common 3 page path for all the pages on the website.
Example log file:
source Target
1 2
1 3
2 1
3 2
3 2
2 1
The most common 3 page path here was from 3 to 2 to 1.
Does anybody have useful example of "this" assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself.
Thank you!
A friend of mine was asked the following question today at interview for the position of software developer.
Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ?
Example: if s1 = "stackoverflow"; then the following are some of its rotated versions:
"tackoverflows"
"ackoverflowst"
"overflowstack"
where as "stackoverflwo" is not a rotated version.
The answer he gave was:
Take s2 and find the logest prefix that is a substring of s1, that will give you the point of rotation. Once you find that point, break s2 at that point to get s2a and s2b, then just check if concatenate(s2a,s2b) == s1
It looks like a good solution to me and my friend. But the interviewr though otherwise. He asked for a simpler solution. Please help me by telling how would you do this in Java/C/C++ ?
Thanks in advance.
I am trying to have a datagridview with two comboboxes, a company name and a supplier account number.
When a company name is selected the relevant supplier account numbers (a company can have more than one supplier account) should be filtered for that company in that row.
I have a datagridview with two bindingsources: supplierBindingSource and companyBindingSource and the Supplier account combobox uses the supplierBindingSource for its datasource and company name uses the companyBindingSource for its datasource.
A company can have a supplier and/or customer account so the supplierBindingSource is a child or a companyBindingSource.
The supplier accounts correctly filter based on the selected company name however when another company name is selected in another row all the supplier accounts are filtered for that company.
Saving still works properly, regardless of what the combo box show but currently it is very confusing for a user to select a supplier account when the labels don't show what is being saved. Is it possible to have only the selected row to filter based on the company name rather than every row?
EDIT: Thanks to the answer from http://social.msdn.microsoft.com/Forums/windows/en-US/b23d9e8f-a00a-49ba-adf5-52d87c1b2890/parent-child-comboboxes-in-datagridview
I have been able to make some progress. The supplier account gets filtered when the drop down box is selected and restored to the full list when selection is finished.
However now I am trying to have the company selected (and not filtered) when a supplier account is selected.
The issue I've run into now is that I can get the companyID (which is the valuemember for the company combobox) but I cannot select the appropriate company without just setting the value of the combobox to the companyID which displays the companyID instead of the company name.
Is there a way to select the company using the companyID while preserving the displaymember/valuemember dynamic?
EDIT 2: The wall of text may be off putting. Some code to help elucidate my issue
DataGridViewComboBoxCell dgcb2 = (DataGridViewComboBoxCell)sdgvSalesOrderLines[cmbSupplierName.Index, e.RowIndex];
var companyID = col.FirstOrDefault(c => c.AccountID == Convert.ToInt32(dgcb.Value)).CompanyID;
dgcb2.Value = companyID.toString();
The second line gets the companyID and that works just fine, the final line sets the combobox to display the companyID whereas I would like it to set the valuemember value to companyID so that it would display the corresponding company name.
I can set the second line to give me the company name instead but if the value of the combobox isn't the companyID then the supplier account cannot filter based on the company selected.
hi,
AFAIK, string literals are stored in read only memory in case of C language.
where is this actually present on the hardware.
as per my knowledge heap is on RAM.correct me if i am wrong.
how different is heap from read only memory?
is it OS dependant?
I have a long string for example it could be "aaaaaabbccc". Need to represent it as "a6b2c3". What's the best way to do this ? I could do this in linear time by comparing characters and incrementing counts and then replacing the counts in the array, using two indexes in one pass. Can you guys think of a better way than this? Are any of the encoding techniques going to work here ?
Pretend you're at an interview. The interviewer asks:
What is the decimal value of this binary number?
10110101
What is the first thing you responsed with?
I was given this interview question recently:
An analog clock shows that the current time is HH:MM. Compute in degree, the smaller angle between the hour and minute hands. Be as precise as you can.
I'm wondering what's the simplest, most readable, most precise algorithm is. Solution in any language is welcome (but do explain it a bit if you think it's necessary).
if we have some search terms with frequency, how do I suggest the top 5 frequently searched words? The data structure used must be easily updatable.(for eg: if I want to update frequency of a term or add a new term etc...)
Generate a random number in range [x..y] where x and y are any arbitrary floating point numbers. Use function random(), which returns a random floating point number in range [0..1] from P uniformly distributed numbers (call it "density"). Uniform distribution must be preserved and P must be scaled as well.
I think, there is no easy solution for such problem. To simplify it a bit, I ask you how to generate a number in interval [-0.5 .. 0.5], then in [0 .. 2], then in [-2 .. 0], preserving uniformness and density? Thus, for [0 .. 2] it must generate a random number from P*2 uniformly distributed numbers.
The obvious simple solution random() * (x - y) + y will generate not all possible numbers because of the lower density for all abs(x-y)>1.0 cases. Many possible values will be missed. Remember, that random() returns only a number from P possible numbers. Then, if you multiply such number by Q, it will give you only one of P possible values, scaled by Q, but you have to scale density P by Q as well.
Hello All....
I am just refreshing the oops features of the java. So, I have a little confusion regarding inheritance concept. For that I have a following sample code :
class Super{
int index = 5;
public void printVal(){
System.out.println("Super");
}
}
class Sub extends Super{
int index = 2;
public void printVal(){
System.out.println("Sub");
}
}
public class Runner {
public static void main(String args[]){
Super sup = new Sub();
System.out.println(sup.index+",");
sup.printVal();
}
}
Now above code is giving me output as : 5,Sub.
Here, we are overriding printVal() method, so that is understandable that it is accessing child class method only.
But I could not understand why it's accessing the value of x from Super class...
Thanks in advance....
Hi
This was the question asked in interview, null pointer exception is very common why it is not declared as checked exception.I googled but did not get proper answer.
Thanks
Given a Sorted Array which can be rotated find an Element in it in minimum Time Complexity.
eg : Array contents can be [8, 1, 2, 3, 4, 5]. Assume you search 8 in it.
One of my friend been asked with a question, Retrieving the max top 100 numbers from one hundred million of numbers, in a recent job interview. Do you have any idea to come up with an efficient way to solve it?
regards!