Is it possible to find middle element of a linked list by single pass and single pointer? - singly-linked-list

I was asked in interview, i had given multiple solutions by 2 pointers, hashtable and bruteforce method. But again interviewer asked me to do by single pass and single pointer. I said its impossible!. Let me know if anybody have any idea on this.

Related

Subsets of similar objects by absolute decimal difference

Greetings!
I have the question below, which I solved with a naive way of nested loops, and I'm looking for a better one. Tried looking at counting sort but not sure how will it work with decimals. Thinking some dynamic programing is needed here.
Anyone who can provide an answer with a better running time than O(N^2), please do :)
The Question:
Given list of objects with an X(double) and Y(double) values, and an ID(string) value.
Objects are considered “similar” if the X and Y values of both objects are within 1.5 units, respectively.
Print all objects ID's with no similar objects.
Print the ID of the object with the most similar objects to it, and each similar object under it.

Groovy: Iterating a list

I was trying to iterate a list and after googling, I found one solution like below:
value([0,1,2,3,4,5,6].sort{new Random()}?.take(1)[0])
I did not understand this part:
sort{new Random()}
Can someone explain this?
And which class the take method is belonging to?
sort receives a closure to determine the order that you wish. In this example, it makes a random sort.
take comes from Iterable and returns the first n elements.

Python: What is the most pythonic way of making an item a list if it is not a list

def ensure_list(papers):
if type(papers) is not list:
papers = [papers]
return papers
papers = 1
try:
papers.append(7)
except:
print "not a list"
papers = ensure_list(papers)
papers.append(5)
for item in papers:
print item
This question is asking what is the best way to check and change a variable to a type if it is not.
Is there a better way to make papers a list with the object as its first element? I am writing some code that may be given an object or a list of objects and if it is an object I want it to be appended to a list and then handled.
This is only done once so it seems like over kill to have a separate function for this.
Edit:
The elements will always be a known type (custom class), and always be a list of custom type object or custom type object .
Here the answer: (Thank you Random832)
papers if isinstance(papers, list) else [papers]
Edit2: this is not a duplicate of the other question as this is not asking how to check if something is a list. This question is asking what is the best way to check and change a variable to a type if it is not. This would apply to any type we like really, the general answer is to inline with isinstance. The answer to this specific question question is the inlined statement provided by Random832 nowhere in that other question is that listed. Please read and understand the question before you flag it.
Here it is inlined: (Thank you Random832)
papers if isinstance(papers, list) else [papers]
The best thing to do is to (re)design (some of) your program so that previous functions return the type you need them to be. (Thank you tenwest)

Matching expression to data types involving dynamic 2d arrays

Okay this is a homework question I am having trouble with. I finished all the other questions and this is the last one I'm stuck on.
Assume variable parkingLot points to a dynamic 2D array of Car objects. Match each expression to its data type. One answer will be used twice.
parkingLot[0][2]
parkingLot+1
parkingLot
parkingLot[1]
Match to:
a. Car*
b. Car**
c. Car
Can someone explain this one to me?
Don't want to answer a homework question, so I'll try to be general. You have to think about it as levels. At the top level, you're looking down on the 2D array and you'll see both pointers. If you drill down one level, you'll still see the other pointer. At the bottom is the object itself.

Could the S of S.O.L.I.D be extended for every single element of the code?

The S of the famous Object Oriented Programming design stands for:
Single responsibility principle, the notion that an object should have
only a single responsibility.
I was wondering, can this principle, be extended even to arrays, variables, and all the elements of a program?
For example, let's say we have:
int A[100];
And we use it to store the result of a function, but somehow we use the same A[100] to check, for example, what indexes of A have we already checked and elaborated.
Could this be considered wrong? Shouldn't we create another element to store, for example, the indexes that we have already checked? Isn't this an hint of future messy code?
PS: I'm sorry if my question is not comprehensible but English is not my primary language. If you have any problem understanding the point of it please let me know in a comment below.
If same A instance is used in different program code portions you must follow this principle. If A is a auxiliary variable, local one for example, I think you don't need to be care about it.
If you are tracking the use of bits of the array that have been updated, then you probably shouldn't be using an array, but a map instead.
In any case, if you need that sort of extra control over the array, then basically, you should be considering a class that contains both the contents of the array and the various information about what has and hasn't been done. So your array becomes local to the class object, as do your controls, and voila. You have single responsibility again.