• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 14:18
CET 20:18
KST 04:18
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview7RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump1Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3
StarCraft 2
General
RSL Revival - 2025 Season Finals Preview Weekly Cups (Dec 1-7): Clem doubles, Solar gets over the hump Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced
Tourneys
RSL Offline Finals Info - Dec 13 and 14! Tenacious Turtle Tussle 2025 RSL Offline Finals Dates + Ticket Sales! Sparkling Tuna Cup - Weekly Open Tournament StarCraft2.fi 15th Anniversary Cup
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 504 Retribution Mutation # 503 Fowl Play Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ [BSL21] RO8 Bracket & Prediction Contest BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle Let's talk about Metropolis
Tourneys
[ASL20] Grand Finals [BSL21] RO8 - Day 2 - Sunday 21:00 CET [BSL21] RO8 - Day 1 - Saturday 21:00 CET Small VOD Thread 2.0
Strategy
Simple Questions, Simple Answers Game Theory for Starcraft Fighting Spirit mining rates Current Meta
Other Games
General Games
Dawn of War IV Path of Exile Stormgate/Frost Giant Megathread Awesome Games Done Quick 2026! Nintendo Switch Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas Survivor II: The Amazon Sengoku Mafia TL Mafia Community Thread
Community
General
Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine US Politics Mega-thread YouTube Thread European Politico-economics QA Mega-thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
TL+ Announced Where to ask questions and add stream?
Blogs
How Sleep Deprivation Affect…
TrAiDoS
I decided to write a webnov…
DjKniteX
James Bond movies ranking - pa…
Topin
Thanks for the RSL
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1672 users

Logic/Programming Interview Questions - Page 3

Blogs > Oracle
Post a Reply
Prev 1 2 3 All
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
June 27 2011 14:05 GMT
#41
Frigo, the O(1) space requirement implies that the additional space we need isn't dependent on how large the input is. If you are using 1 extra bit for every node, that could theoretically be 2^100 more bits. That's why there is a significant difference between in-place and non in-place sorting algorithms: If you have a petabyte of data and a petabyte of storage space, you're not going anywhere without an in-place algorithm.

But given the way the question is worded, we have no guarantee about the current state of the pointers, as an adversary could defeat you every time on the first node.
Compilers are like boyfriends, you miss a period and they go crazy on you.
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 20:44:45
June 27 2011 20:42 GMT
#42
Sorry I havent checked this in a while, here's some clarifications:

I will post a diagram for Q2 soon

BottleAbuser, your proof of the O(n) running time of k-select is fine with me now

I have a different solution:
You turn the array into a heap and then find the k-th largest. I'll post a detailed solution soon.

For Q6:
You can assume that node->next modulo 4 == 0 for every node in the list. You guys are actually really close to the answer.

Frigo, your answer to Q7 doesn't seem right with me, you can refer to the post I made before regarding a similar method.
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:42:40
June 27 2011 21:26 GMT
#43
heapify(A)
A: an array

1. n ← size(A) − 1
2. for i ← floor(n/2) to 0 do
3. bubble−up(A, i)

Builds a max-heap in linear-time, given the assumption that the 2i'th index is the left child, and 2i+1'th index is the right child of an element.

Then its a simple o(k) time to find the kth largest
Hamster1800
Profile Blog Joined August 2008
United States175 Posts
June 27 2011 22:02 GMT
#44
Could you elaborate more on how to select once you've heapified the array? I'm not familiar with a linear time method to do k-select in a heap. BottleAbuser's method (recurse on median-of-medians) I believe is the standard method for k-select.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:37:46
June 27 2011 22:09 GMT
#45
You do sort of a breadth first search on the heap, giving priority to the greatest element and traverse that node. It comes out to O(k)
Hamster1800
Profile Blog Joined August 2008
United States175 Posts
June 27 2011 22:19 GMT
#46
That shouldn't work. The relative size of two nodes says nothing about the relative sizes of their children. For example, just because the top of the (min-)heap is 1 with children 10 and 100, the 20th smallest element might be the 100. It also could be some deep descendent of the 10 or a deep descendent of the 100. I don't understand how you're going to determine which is the case.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:44:38
June 27 2011 22:22 GMT
#47
A max heap is a left-justified binary tree. The invariant on the tree is that a node's key is strictly greater than or equal to each of its children nodes' key.

We traverse only k-1 edges, and every time we traverse an edge we will effectively be eliminating a greater element than k. But we only traverse the edge with the child with the greatest key.

Hence we place each key in a priority queue, and traverse the greatest key out of all the "active" nodes.

But since we only traverse k-1 edges, we are done at the kth iteration.


In Pseudocode,

activenode <- root
initialize Maxheap Q

for 0 to k-1
put children of activenode into Q
activenode <- get max of Q

return activenode

Actually i guess this is O(klogk) but I believe theres an analysis which shows its within n so i'll think about it for a bit. (Something to do with Q's size being at most n/2 iirc, guess I shouldve done the problems before posting them )

Hamster1800
Profile Blog Joined August 2008
United States175 Posts
Last Edited: 2011-06-27 22:27:47
June 27 2011 22:27 GMT
#48
Let me be more clear: I'm not worried about the runtime of your algorithm. I'm worried about what it is and that it gets the correct answer. If you could explain your k-select method in full and explain how it gets the kth largest element, that would answer my question.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
June 27 2011 22:38 GMT
#49
Ive edited my post above to be more clear
Frigo
Profile Joined August 2009
Hungary1023 Posts
Last Edited: 2011-06-28 03:02:28
June 28 2011 02:59 GMT
#50
On June 28 2011 05:42 Oracle wrote:
For Q6:
You can assume that node->next modulo 4 == 0 for every node in the list. You guys are actually really close to the answer.

Well it's settled then. We just need to set a bit in the "next" pointer to mark a node visited. And technically, the algorithm uses only an O(1) space overhead, not O(n), made possible by the special structure of the list.

Frigo, your answer to Q7 doesn't seem right with me, you can refer to the post I made before regarding a similar method.

Well, I have only shown that we convey enough information to tell the 5th card, I left the ordering and indexing of the cards, and the method to calculate the index of the 5th from the possible arrangements, implicit.

Other than that, I forgot the case when all cards are upside down:
0 upside down, 4 visible: 4 choose 0 * 4! = 1 * 24 = 24
1 upside down, 3 visible: 4 choose 1 * 3! = 4 * 6 = 24
2 upside down, 2 visible: 4 choose 2 * 2! = 6 * 2 = 12
3 upside down, 1 visible: 4 choose 3 * 1! = 4 * 1 = 4
4 upside down, 0 visible: 4 choose 4 * 0! = 1 * 1 = 1
65 possible arrangements
http://www.fimfiction.net/user/Treasure_Chest
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
Last Edited: 2011-06-28 11:17:25
June 28 2011 11:15 GMT
#51
Well, we can simply increment each traversed node's next pointer to mark it as visited then. If the next node'spointer mod 4 is 1 then that's the start of the loop.

Should read whole thread heh. I meant "what frigo said"
Compilers are like boyfriends, you miss a period and they go crazy on you.
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
June 28 2011 12:15 GMT
#52
On June 25 2011 09:32 Oracle wrote:
Here's mine:

We think of a card as a {0,1}-bit. A card represents 0 if it is face up and 1 if it is face-down.

So then we get a binary string of length four. So we can represent 2^4 - 1 = 15 possibly values with a string of 4 bits. But there are really only 13 distinct values. Hence we will never get the string 1111. So we will always have a face-up card.

So we assign 'a' = Hearts, 'b' = Diamonds, 'c' = Spades, 'd' = Clubs. The card which is face-up and leftmost denotes the suit.


So for example A F F F can be read as 0111 = 7. The A says its the 7 of Hearts.
FBDF can be read as 1001 = 9. The B says its the 9 of Diamonds.

Maybe I'm just misunderstanding your method, but doesn't this assume you actually get at least one other card of the same suit as the fifth card? For example how would you represent the 7 of Hearts if the other 4 cards are black?
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-28 15:02:31
June 28 2011 15:01 GMT
#53
On June 28 2011 21:15 iaretehnoob wrote:
Show nested quote +
On June 25 2011 09:32 Oracle wrote:
Here's mine:

We think of a card as a {0,1}-bit. A card represents 0 if it is face up and 1 if it is face-down.

So then we get a binary string of length four. So we can represent 2^4 - 1 = 15 possibly values with a string of 4 bits. But there are really only 13 distinct values. Hence we will never get the string 1111. So we will always have a face-up card.

So we assign 'a' = Hearts, 'b' = Diamonds, 'c' = Spades, 'd' = Clubs. The card which is face-up and leftmost denotes the suit.


So for example A F F F can be read as 0111 = 7. The A says its the 7 of Hearts.
FBDF can be read as 1001 = 9. The B says its the 9 of Diamonds.

Maybe I'm just misunderstanding your method, but doesn't this assume you actually get at least one other card of the same suit as the fifth card? For example how would you represent the 7 of Hearts if the other 4 cards are black?

The 1st card I pulled out is mapped to hearts, regardless if it is a black-suited card. So we map the ordering not the suit.


On June 28 2011 20:15 BottleAbuser wrote:
Well, we can simply increment each traversed node's next pointer to mark it as visited then. If the next node'spointer mod 4 is 1 then that's the start of the loop.

Should read whole thread heh. I meant "what frigo said"



Right, I love this one
Prev 1 2 3 All
Please log in or register to reply.
Live Events Refresh
Next event in 43m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 397
IndyStarCraft 170
JuggernautJason128
StarCraft: Brood War
Britney 19891
Calm 2879
Shuttle 440
Mini 394
EffOrt 229
Dewaltoss 98
ggaemo 87
Oya187 46
Dota 2
Gorgc6177
qojqva2871
LuMiX1
Counter-Strike
fl0m10428
byalli506
chrisJcsgo49
Super Smash Bros
hungrybox224
Heroes of the Storm
Khaldor625
Liquid`Hasu449
Other Games
Beastyqt653
ArmadaUGS105
XaKoH 82
OptimusSC22
Organizations
Other Games
gamesdonequick1110
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• HeavenSC 48
• Adnapsc2 14
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV395
League of Legends
• Nemesis2660
Other Games
• imaqtpie1807
• Shiphtur745
Upcoming Events
IPSL
43m
Sziky vs JDConan
BSL 21
43m
Tech vs Cross
Bonyth vs eOnzErG
Replay Cast
13h 43m
Wardi Open
16h 43m
Monday Night Weeklies
21h 43m
Sparkling Tuna Cup
1d 14h
Replay Cast
3 days
The PondCast
3 days
CranKy Ducklings
5 days
SC Evo League
5 days
[ Show More ]
BSL 21
6 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

Acropolis #4 - TS3
RSL Revival: Season 3
Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
Slon Tour Season 2
WardiTV 2025
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22

Upcoming

CSL 2025 WINTER (S19)
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Big Gabe Cup #3
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.