Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

parsing interview question

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You have the String array:
String[] array = new String[]{....};
The size of the array is dynamic as well as the size of the strings in that array.
All of the strings are concatenated to create one long string.
array = null;
How would you create the array which structure will be the same?
You can not save the sizes of the strings. In that strings every sign can be used.
 
Marshal
Posts: 77529
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I understand the question. Tell us what you said, then maybe we can understand it.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding: Re-create the original array precisely given only the string.
 
Campbell Ritchie
Marshal
Posts: 77529
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that mean you use a particular sign or set of signs to catenate the Strings? If those signs are distinct from the remainder of the String, that actually sounds an easy exercise.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If "you can not save the lengths of the strings" only means that you must not save the lengths in external variables, you could also code each string length (in a certain, fixed length format ;) before each string. That way you wouldn't have to worry about separator chars or sequences.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My take was that it wasn't delimited (which is why I didn't post an answer ;)
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:My understanding: Re-create the original array precisely given only the string.


Exactly.

Ogranos, what do you have on your mind? You can not save the length of particular strings.
Let's say that we have an array of Strings, then we create one long string, send it using RMI and the receiver has to create original array.
 
Sheriff
Posts: 22725
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C the '\0' character is used to indicate the end of each string. It is also used as the end of the array itself. For instance, string array {"abc", "def", "ghi"} would be represented as "abc\0def\0ghi\0\0".

In Java you won't need that final array terminator, but you will need something similar between each elements. You could use '\0', or perhaps '\036' or '\037' - the record and unit separator characters. In the end it doesn't matter, as long as you choose a character that is not part of the strings themselves.

Now, if you don't have control over how this string is built then you're screwed. From just "abcdefghi" you can never know what the original was. It could have been {"abc", "def", "ghi"}, or {"ab", "cd", "ef", "gh", "i"}, or any other combination.

For RMI you won't need this though; RMI can send arrays without any problem, provided that each element is serializable.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lucas Smith wrote:Ogranos, what do you have on your mind? You can not save the length of particular strings.
Let's say that we have an array of Strings, then we create one long string, send it using RMI and the receiver has to create original array.



Well you didn't say what you had to use this for, so I assumed that it was a (homework) assignment where you were said "don't save the string lenghts in any extra variables". But you could still save the length of each string by coding it into the long string, for example

String[] ar = { "Test", "Huh" }

could become the String "0004Test0003Huh" if you code the length of each string as a 4 char hexadecimal number (it is also helpful to code the length of the array in the same way). Then the receiver just has to read out the lenghts and can determine how many chars of actual text follow.

But as Rob said, this probably won't be needed if you can send arrays over RMI.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic